C:文件操作-读写字符与读写行(!!!好)

来自:http://zhangjunhd.blog.51cto.com/113473/100299 

 

 

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);
}

你可能感兴趣的:(c,filter,File,null,存储,FP)