C_俩文件打印不同行

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLINE 100

// 比较:比较俩个文件,输出第一个不同的行
int main(int argc, char *argv[])
{
    FILE *fp1, *fp2;
    void filecomp(FILE *fp1, FILE *fp2);

    if (argc != 1){
        fprintf(stderr, "comp: need two file names\n");
        exit(1);
    }else {
        if ((fp1 = fopen("C:\\Users\\admin\\Desktop\\a\\c\\3.txt", "r")) == NULL){
            fprintf(stderr, "comp: can't open %s\n", *argv);
            exit(1);
        }else if ((fp2 = fopen("c:\\2.txt", "r")) == NULL){
            fprintf(stderr, "comp: can't open %s\n", *argv);
            exit(1);
        }else {
            filecomp(fp1, fp2);
            fclose(fp1);
            fclose(fp2);
            exit(0);
        }
    }
}

void filecomp(FILE *fp1, FILE *fp2)
{
    char line1[MAXLINE], line2[MAXLINE];
    char *lp1, *lp2;

    do {
        lp1 = fgets(line1, MAXLINE, fp1);
        lp2 = fgets(line2, MAXLINE, fp2);
        if (lp1 == line1 && lp2 == line2){
            if (strcmp(line1, line2) != 0){
                printf("first difference in line\n%s\n", line1);
                lp1 = lp2 = NULL;
            }
        }else if (lp1 != line1 && lp2 == line2)
            printf("end of first file at line\n%s\n", line2);
        else if (lp1 == line1 && lp2 != line2)
            printf("end of second file at line\n%s\n", line1);
    }while (lp1 == line1 && lp2 == line2);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值