1.讀寫字符函數putc()與getc()
這兩個函數類似於putchar()與getchar()函數。假設fp是一個FILE指針,ch是一個字符變量,
ch = getc(fp);// ch = getchar();
putc(ch,fp);// putchar(ch);
將文件內容(按字符)輸出到標准輸出的C實現:
#include <stdio.h>
#include <stdlib.h>
/* 將文件內容(按字符)輸出到標准輸出 */
void read_ch(char * filename, char * mode)
{
int ch;
FILE * fp;
if ((fp = fopen(filename,mode)) == NULL)
{
fprintf(stderr,"Can't open %s.\n",filename);
exit(1);
}
while ((ch = getc(fp)) != EOF)
putc(ch,stdout);
fclose(fp);
}
2.讀寫行函數fgets()與fputs()
gets()函數只需要一個參數(char *),而fgets()函數需要三個參數,(char *, int, FILE *)。第一個參數是用於存儲輸入的地址,第二個參數表示字符串的最大長度,最後一個參數是FILE指針。通常是:
fgets(buf, MAX, fp);//gets(buf);
fgets()函數讀取到它所遇到的第一個換行符的後面,或者讀取比字符串的最大長度少一個字符,或者讀取到EOF。然後它會在末尾添加一個'\0',這就構成了一個字符串。(這和gets()不同,它會讀取到'\n'之前,並添加一個'\0',而再次讀取時,會丟棄'\n')。與gets()類似,fgets()遇到EOF時會返回NULL,否則返回傳遞給它的地址。
fputs()函數接受兩個參數,(char *, FILE *)。與puts()不同的是,它不會自動添加'\n'。通常是:
fputs(buf, fp);//puts(buf);
將文件內容(按行)輸出到標准輸出的C實現:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 21
/* 將文件內容(按行)輸出到標准輸出 */
void read_line(char * filename, char * mode)
{
char line[MAXLINE];
FILE * fp;
if ((fp = fopen(filename,mode)) == NULL)
{
fprintf(stderr,"Can't open %s.\n",filename);
exit(1);
}
while (fgets(line, MAXLINE,fp) != NULL)
fputs(line,stdout);
fclose(fp);
}
3.分別以按字符和按行實現:以行為單位,交替輸出兩文件內容
/* 以行為單位,交替輸出兩文件(按字符)內容 */
void cross_read_ch(char * file1, char * file2, char * mode)
{
FILE * fp1, * fp2;
char ch1, ch2;
if ((fp1 = fopen(file1,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file1);
exit(1);
}
if ((fp2 = fopen(file2,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file2);
exit(1);
}
ch1 = getc(fp1);
ch2 = getc(fp2);
while (ch1 != EOF && ch2 != EOF)
{
while (ch1 != EOF && ch1 != '\n')
{
putc(ch1,stdout);
ch1 = getc(fp1);
}
if (ch1 != EOF)//文本1到達行尾
{
putc('\n',stdout);
ch1 = getc(fp1);
}
while (ch2 != EOF && ch2 != '\n')
{
putc(ch2,stdout);
ch2 = getc(fp2);
}
if (ch2 != EOF)//文本2到達行尾
{
putc('\n',stdout);
ch2 = getc(fp2);
}
}
while (ch1 != EOF)
{
putc(ch1,stdout);
ch1 = getc(fp1);
}
while (ch2 != EOF)
{
putc(ch2,stdout);
ch2 = getc(fp2);
}
fclose(fp1);
fclose(fp2);
}
/* 以行為單位,交替輸出兩文件(按行)內容 */
void cross_read_line(char * file1, char * file2, char * mode)
{
FILE * fp1, * fp2;
char line1[MAXLINE];
char line2[MAXLINE];
if ((fp1 = fopen(file1,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file1);
exit(1);
}
if ((fp2 = fopen(file2,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file2);
exit(1);
}
while (fgets(line1,MAXLINE,fp1) != NULL && fgets(line2,MAXLINE,fp2) != NULL)
{
fputs(line1,stdout);
fputs(line2,stdout);
}
while (fgets(line1,MAXLINE,fp1) != NULL)
fputs(line1,stdout);
while (fgets(line2,MAXLINE,fp2) != NULL)
fputs(line2,stdout);
fclose(fp1);
fclose(fp2);
}
4.分別以按字符和按行實現:以行為單位,將兩文件相同行內容並列輸出
/* 以行為單位,將兩文件(按字符)相同行內容並列輸出 */
void cat_read_ch(char * file1, char * file2, char * mode)
{
FILE * fp1, * fp2;
char ch1, ch2;
if ((fp1 = fopen(file1,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file1);
exit(1);
}
if ((fp2 = fopen(file2,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file2);
exit(1);
}
ch1 = getc(fp1);
ch2 = getc(fp2);
while (ch1 != EOF && ch2 != EOF)
{
while (ch1 != EOF && ch1 != '\n')
{
putchar(ch1);
ch1 = getc(fp1);
}
if (ch1 != EOF)//文本1到達行尾
{
if (ch2 == EOF)
putc('\n',stdout);
ch1 = getc(fp1);
}
while (ch2 != EOF && ch2 != '\n')
{
putchar(ch2);
ch2 = getc(fp2);
}
if (ch2 != EOF)//文本2到達行尾
{
putc('\n',stdout);
ch2 = getc(fp2);
}
}
while (ch1 != EOF)
{
putchar(ch1);
ch1 = getc(fp1);
}
while (ch2 != EOF)
{
putchar(ch2);
ch2 = getc(fp2);
}
fclose(fp1);
fclose(fp2);
}
/* 過濾字符串中的'\n' */
static void filter(char * line)
{
int len = strlen(line);
*(line + len -2) = '\0';
}
/* 以行為單位,將兩文件(按行)相同行內容並列輸出 */
void cat_read_line(char * file1, char * file2, char * mode)
{
FILE * fp1, * fp2;
char line1[MAXLINE];
char line2[MAXLINE];
if ((fp1 = fopen(file1,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file1);
exit(1);
}
if ((fp2 = fopen(file2,mode)) == NULL){
fprintf(stderr,"Can't open %s\n",file2);
exit(1);
}
while (fgets(line1,MAXLINE,fp1) != NULL && fgets(line2,MAXLINE,fp2) != NULL)
{
filter(line1);
fputs(line1,stdout);
fputs(line2,stdout);
}
while (fgets(line1,MAXLINE,fp1) != NULL)
fputs(line1,stdout);
while (fgets(line2,MAXLINE,fp2) != NULL)
fputs(line2,stdout);
fclose(fp1);
fclose(fp2);
}
本文出自 “子 孑” 博客,請務必保留此出處http://zhangjunhd.blog.51cto.com/113473/100299