题目——三个文件

来源:互联网 发布:水货导师 知乎 编辑:程序博客网 时间:2024/06/06 09:45

这里写图片描述

代码:

/*****************************************************     File name:zuo_ye.c Author: Tang Zhiqian  Date:2017-08-12 17:16*****************************************************/#include <stdio.h>#define MAX 10int main(int argc,char **argv){    if (argc != 4)    {        printf("please input: a.out filename1 filename2 filename3\n");        return -1;    }    FILE *fp1,*fp2,*fp3;    if((fp1 = fopen(argv[1],"r"))== NULL)    {        perror("open file1");        return 1;    }    if((fp2 = fopen(argv[2],"r"))== NULL)    {        perror("open file2");        return 2;    }    if((fp3 = fopen(argv[3],"w"))== NULL)    {        perror("open file3");        return 3;    }    char ch1,ch2,ch3;    int a,b;    while(!feof(fp1))     {        ch1 = fgetc(fp1);        if (ch1 == EOF)        {            break;        }        ch2 = fgetc(fp2);        if (ch2 == EOF)        {            break;        }        if (ch1 > '9' || ch1 < '0')        {            fputc(ch1,fp3);        }        else        {            fseek(fp1,-1,SEEK_CUR);   //读到数字退一个指针,然后读整个数字            fscanf(fp1,"%d",&a);            fseek(fp2,-1,SEEK_CUR);            fscanf(fp2,"%d",&b);            fprintf(fp3, "%d ",a + b);        }    }    fclose(fp1);    fclose(fp2);    fclose(fp3);    return 0;}
原创粉丝点击