简体   繁体   English

交替合并来自两个文本文件的文本

[英]Merging text from two text files alternatively

Hi I tried the same using fgets but the result are very different, I am able to achieve it through fgetc but not using fgets, what am I doing wrong.嗨,我使用 fgets 尝试了相同的方法,但结果却大不相同,我可以通过 fgetc 实现它,但不能使用 fgets,我做错了什么。 Somehow while merging a line is skipped, for instance it copies 1st file from each file and then skips to the 3rd line.不知何故,在合并一行时会被跳过,例如它从每个文件中复制第一个文件,然后跳到第三行。

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

int main()
{
    FILE *pointer1, *pointer2, *pointer3;
    char source1[80],source2[80],target[80];
    char str1[80],str2[80];

    printf("Enter the source and source 2\n");
    scanf("%s %s", source1,source2);

    printf("Enter the destination\n");
    scanf("%s",target);

    pointer1 = fopen(source1,"r");
    pointer2 = fopen(source2,"r");
    pointer3 = fopen(target,"w");

    if(pointer1 == NULL || pointer2==NULL || pointer3==NULL) {
        printf("Cannot open a file\n ");
        exit(1);
    }
    while(1) {
        if(fgets(str1,79,pointer1)!=NULL) {
            fputs(str1,pointer3);           
        }
        if(fgets(str1,79,pointer1)==NULL)
            break;
        if(fgets(str2,79,pointer2)!=NULL) {
            fputs(str2,pointer3);                
        }
        if(fgets(str2,79,pointer2)==NULL)
            break;
    }

    fclose(pointer1);
    fclose(pointer2);
    fclose(pointer3);

    printf("Merging completed successfully\n");
    printf("Press any key to exit\n");
    getch();
}

The unexpected behaviour you are getting is actually very expected.你得到的意外行为实际上是非常预期的。 Even if you are, in your mind, just testing if the file is not done yet, eg: if(fgets(..) != NULL) , that function actually extracts that line from a file and loses it, since you don't do anything with it.即使您在脑海中只是测试文件是否尚未完成,例如: if(fgets(..) != NULL) ,该函数实际上会从文件中提取该行并丢失它,因为您没有不要用它做任何事情。

First, I have two mentions:首先,我要提两点:

  1. At least on my computer, you need to include "conio.h" for the getch function, so I don't know how it compiles on your PC.至少在我的电脑上,你需要为 getch 函数包含“conio.h”,所以我不知道它是如何在你的电脑上编译的。
  2. For future reference, please check a coding-style guide, because there are very many mistakes.为了将来参考,请检查编码风格指南,因为有很多错误。 Try this link: https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html .试试这个链接: https : //users.ece.cmu.edu/~eno/coding/CCodingStandard.html For a simpler method, just search on google: "C code beautify" and they will take care of the coding style the code for you.对于更简单的方法,只需在谷歌上搜索:“C 代码美化”,他们会为您处理代码的编码风格。

I'll help you with a solution here, but, to not make it too easy, I've also slipped a little mistake.我会在这里帮助你解决一个问题,但是,为了不让它太容易,我也犯了一个小错误。

#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#include<conio.h>

int main() {
    FILE *pointer1, *pointer2, *pointer3;
    char source1[80], source2[80], target[80];
    char str1[80], str2[80];

    printf("Enter the source and source 2\n");
    scanf("%s %s", source1, source2);

    printf("Enter the destination\n");
    scanf("%s", target);

    pointer1 = fopen(source1, "r");
    pointer2 = fopen(source2, "r");
    pointer3 = fopen(target, "w");

    if (pointer1 == NULL || pointer2 == NULL || pointer3 == NULL) {
        printf("Cannot open a file\n ");
        exit(1);
    }
    
    int end_first_file = 0;
    int end_second_file = 0;

    while (1) {
       
        if (fgets(str1, 79, pointer1) != NULL && end_first_file == 0) {
            fputs(str1, pointer3);
        } else {
            end_first_file = 1;
        }
        
        if (fgets(str2, 79, pointer2) != NULL && end_second_file == 0) {
            fputs(str2, pointer3);
        } else {
            end_second_file = 1;
        }
        
        if(end_second_file && end_first_file)
            break;
    }

    fclose(pointer1);
    fclose(pointer2);
    fclose(pointer3);

    printf("Merging completed successfully\n");
    printf("Press any key to exit\n");
    getch();
}

The mistake that I've told you about, that I'm having you solve, is the following:我告诉过你的错误,我让你解决,如下:

Taking as example:以:

Source1:来源1:
aa aa
aaa啊啊啊啊

Source2:来源2:
bb bb
bbb bbb
bbbb bbbb

You will get the output:你会得到输出:
aa aa
bb bb
aaabbb啊啊啊啊
bbbb bbbb

Q1: What happened there? Q1:那里发生了什么? "aaabbb". “啊啊啊”。
Q2: How do you solve it? Q2:你是怎么解决的?

Kind regards, Claudiu亲切的问候,克劳迪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM