0

I have two text file as;

Andrew Hall
Arnold Price
Shelley Baker

and,

Arnold Hill
Veronica Clay

As you can see they are ordered. I need to combine them into another text file which is ordered again. So, expected output is;

Andrew Hall
Arnold Hill
Arnold Price 
Shelley Baker
Veronica Clay

However, the output shows up as;

Andrew Hall
Arnold Hill
Arnold Price

I think somehow I am losing last lines of both files and both fsort1 and fsort2 reach end of their files. How can I find a general solution? What am I doing wrong?

My code is like that;

fgets(name1, 100, fsort1); 
fgets(name2, 100, fsort2);

while(!feof(fsort1) || !feof(fsort2)){
    if(strcmp(name1, name2)<0){
        fprintf(foutput, "%s", name1);
        fgets(name1, 100, fsort1);
    }
    else{
        fprintf(foutput, "%s", name2);
        fgets(name2, 100, fsort2);
    }
}

Thank you.

  • 3
    Possible duplicate of [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle Dec 22 '17 at 14:16
  • 1
    See [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle Dec 22 '17 at 14:17
  • Also, `!feof(fsort1) || !feof(fsort2)` is not an easy-to-read logical condition. It would be easier to read `!(feof(fsort1) && feof(fsort2))`. Yet when the end of one file is reached, the code continues to attempt to fetch input from both files and uses the results as if those results are correct. – ad absurdum Dec 22 '17 at 14:50
  • @AndrewHenle I understand why it is wrong but the things that they expressed as alternative way to do this do not end up what I want. Do you have any suggestion? – Doğukan Arslan Dec 22 '17 at 14:53
  • @DavidBowling I thought that one too but it became "more wrong" than this one. – Doğukan Arslan Dec 22 '17 at 14:54
  • The two logical conditions I wrote above are _logically equivalent_. One can't be more wrong than the other. But this is the wrong logic for your goal. Think about the logic, and read the link suggested by @AndrewHenle. – ad absurdum Dec 22 '17 at 14:57

1 Answers1

1

I think somehow I am losing last lines of both files and both fsort1 and fsort2 reach end of their files.

Yes, you are. The comments already pointed out the wrong use of feof, but if your loop stops because only one of the file is ended, you are not continuing the read the other file. You can use somthing like this:

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


int main(void)
{
    FILE *fsort1 = fopen("names1.txt", "r");
    FILE *fsort2 = fopen("names2.txt", "r");
    FILE *foutput = fopen("names_out.txt", "w");

    if ( !fsort1 || !fsort2 || !foutput)
    {
        perror("Error openng files");
        exit(EXIT_FAILURE);
    }

    char name1[256] = {'\0'};
    char name2[256] = {'\0'};
    char *r1 = fgets(name1, 256, fsort1);
    char *r2 = fgets(name2, 256, fsort2);

    while ( r1 && r2 )
    {
        if ( strcmp(name1, name2) < 0 ) {
            fprintf(foutput, "%s", name1);
            r1 = fgets(name1, 256, fsort1);
        }
        else {
            fprintf(foutput, "%s", name2);
            r2 = fgets(name2, 256, fsort2);
        }
    }
    while ( r1 )
    {
        fprintf(foutput, "%s", name1);
        r1 = fgets(name1, 256, fsort1);        
    }
    while ( r2 )
    {
        fprintf(foutput, "%s", name2);
        r2 = fgets(name2, 256, fsort2);        
    }        
}
Bob__
  • 12,361
  • 3
  • 28
  • 42