简体   繁体   English

在C中合并两个文件

[英]Merge two files in C

I want to merge two files in C, and stop in the end. 我想在C中合并两个文件,最后停止。

I have a program but it keeps spelling out words even after my file is ended. 我有一个程序,但是即使我的文件结束后,它仍会拼写单词。 For example: 例如:

File 1: 文件1:

a
b
c

File 2: 档案2:

1
2
3
4
5

And the result I get is: 我得到的结果是:

a
1
b
2
c
3
c
4
c
5

And I want: 而且我要:

a
1
b
2
c
3
4
5

I think it's my if statement in my while loop that is my issue. 我认为这是我的while循环中的if语句。 I don't know how to define that. 我不知道该怎么定义。

My code is: 我的代码是:

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


int main(void) {

    char d[200];                        //char för da.txt
    char h[200];                        //char för hej.txt
    FILE *hptr, *dptr;

    hptr = fopen("hej.txt", "r");       //Öppna hej.txt => hptr
    dptr= fopen("da.txt", "r");         //Öppna da.txt => dptr

    /*
    if(((hptr) == NULL && (dptr) == NULL)) //Fall 1 
    {
        printf("Error, båda filerna är tom");
        exit(1);
    }


    if ((hptr) == NULL)                 //Fall 2 när hej.txt är tom
    {
        fscanf(dptr,"%[^/]", d);        //Hämtar från da.txt
        printf("\nFil 2:\n%s", d);      //Skriver ut från da.txt
        exit(1);
    }

    if ((dptr) == NULL)                 //Fall 3 när da.txt är tom
    {
        fscanf(hptr,"%[^/]", h);        //Hämtar från hej.txt
        printf("Fil 1:\n%s", h);        //Skriver ut från hej.txt
        exit (1);
    } */

    if(hptr != NULL || dptr != NULL)        //Fall 4 när ingen fil är tom
    {

        while (!feof (hptr) && !feof (dptr))
        {
            if (hptr ***I guess this is the problem*** == feof)
            {
            fgets(d, 200, dptr);
            printf("%s", d);
            }

            if (hptr == feof)  
            {
            fgets(h, 200, hptr);
            printf("%s", h);
            }

        }

        fclose (hptr);
        fclose (dptr);
    }
    //getch();
    return EXIT_SUCCESS;
}

feof only updates when you try to do some IO. feof仅在您尝试执行IO时更新。 When you call fgets, and it reaches EOF and fails, you just print the string anyway, even though it wasn't updated. 当您调用fgets时,它到达EOF并失败,即使您未更新该字符串,也无论如何都会打印该字符串。 This is why you get the repeated "c" 这就是为什么您得到重复的“ c”的原因

Try this instead: 尝试以下方法:

 while (!feof (hptr) || !feof (dptr))
 {
    if (fgets(d, 200, dptr))
    {
        printf("%s", d);
    }

    if (fgets(h, 200, hptr))  
    {
        printf("%s", h);
    }
}

Try to use 尝试使用

!feof (hptr) 

instead of 代替

hptr == feof

Explanation: feof is a function -- you are comparing a file pointer hptr against a function pointer feof which will never work -- but since you can always compare pointers in C the compiler does not give you any errors. 说明:feof是一个函数-您正在将文件指针hptr与永远不会起作用的函数指针feof进行比较-但由于您始终可以在C中比较指针,因此编译器不会给您任何错误。

You also appears to have a cut-and-paste error as you are using hptr in both if statements -- Im sure one of them should have been dptr 在两个if语句中都使用hptr ,您似乎也出现了剪切和粘贴错误-确保其中之一应该是dptr

Actually you need this: 实际上,您需要这样做:

  if (hptr != NULL && dptr != NULL)        //Fall 4 när ingen fil är tom
  {               //^ you need && here, not ||

    do
    {
        if (fgets(d, 200, dptr) != NULL)
          printf("%s", d);

        if (fgets(h, 200, hptr) != NULL)
          printf("%s", h);
    } while (!(feof(hptr) && feof(dptr)));

    fclose(hptr);
    fclose(dptr);
  }

Please see this SO question: Why is “while ( !feof (file) )” always wrong? 请参阅以下SO问题: 为什么“ while(!feof(file))”总是错误的?

You don't need feof() (it is terrible! ) , just use fgets()s return value, alternating between the two input files. 您不需要feof()太糟糕了! ),只需使用fgets()的返回值,即可在两个输入文件之间交替使用。

#include <stdio.h>

int main(void)
{
char buff[100];
int state;

FILE * one, *two;
one = fopen("one", "r" );
two = fopen("two", "r" );

if (!one || !two) return 1;

for (state=0; ; state ^=1 ) {
        if (!fgets( buff, sizeof buff, state ? two: one)) break;
        printf("%s", buff);
        }

while (fgets( buff, sizeof buff, state? one : two)) {
        printf("%s", buff);
        }
fclose(one);
fclose(two);

return 0;
}

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

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