2

I am trying to read line by line from a file and print out the line. But when I run the code, it starts printing out the lines starting in the middle.

char temp[300];

if (input == NULL) {
    printf("Can't open input file.\n");
    exit(-1);
}

while (!feof(input)) {
    fgets(temp, 300, input);
    printf("%s \n", temp);
}
fclose(input);

Any reason for it to start in the middle?

Edit: So and example of what I mean in the middle is that I have a list like this

7,12 Angry Men,1957
95,2001: A Space Odyssey,1968
211,8 and a Half,1963
190,A Beautiful Mind,2001
68,A Clockwork Orange,1971
223,A Fistful of Dollars,1964
108,A Separation,2011
233,A Streetcar Named Desire,1951
40,Alien,1979
58,Aliens,1986
96,All About Eve,1950
224,All Quiet on the Western Front,1930
250,All the President's Men,1976
91,Amadeus,1984
69,Amelie,2001
54,American Beauty,1999
33,American History X,1998
189,Amores Perros,2000

and when I get to the printf it only shows this

58,Aliens,1986
96,All About Eve,1950
224,All Quiet on the Western Front,1930
250,All the President's Men,1976
91,Amadeus,1984
69,Amelie,2001
54,American Beauty,1999
33,American History X,1998
189,Amores Perros,2000

Edit2: I made a change in the program to get rid of the \n in printf

while (fgets(temp, sizeof(temp), input) != NULL) {
        printf("%s", temp);
    }

and this fixed the problem. Is there any reason why the \n caused this problem?

Nukodi
  • 335
  • 1
  • 9
  • 24
  • Do the following experiment: pipe the output of your program to a file; then look at this file with a (good!) plain text editor. Are all the lines in there? – Jongware Sep 05 '16 at 20:59
  • I opened my file in notepad and compared it with my results which is how I noticed the missing items in the list. – Nukodi Sep 05 '16 at 21:01
  • Well done! (Reason I asked was that 'not visible' could have meant they were overwritten *on the screen only* because of stray carriage returns. I've had that myself.) – Jongware Sep 05 '16 at 21:03
  • the posted 'incorrect' output is not possible. The `\n` in the `printf()` statement, along with the newline read by the call to `fgets()` would have resulted in double spaced output. What kind of `line endings` is your editor set to produce? The best (most common) setting would be `DOS line endings` (otherswise assure the line endings are set for the system you are using) Be sure all the file lines are set to the same line ending. The source of the problem seems to be the the first part of the file line endings were not correct. – user3629249 Sep 06 '16 at 21:26

1 Answers1

3

Take a look to Why is “while ( !feof (file) )” always wrong?

fgets is enough:

while (fgets(temp, 300, input) != NULL) {
    printf("%s \n", temp);
}

Also, don't use magic numbers like 300, change to

while (fgets(temp, sizeof temp, input) != NULL) {
    printf("%s \n", temp);
}

it starts printing out the lines starting in the middle

Notice that fgets includes the trailing newline '\n' and you don't need to include it in the printf, do you mean this by "in the middle"?

Community
  • 1
  • 1
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Thanks! However, I am still not getting the entire list from the file. It still starts in the middle. So if the list was apple, banana, grape, pineapple, kiwi the program only outputs grape, pineapple, kiwi – Nukodi Sep 05 '16 at 20:47
  • Thats really strange, are you sure that you are not using an old version of your file? – David Ranieri Sep 05 '16 at 20:50
  • No, I tried saving, and reopening it and still gave me the same results. – Nukodi Sep 05 '16 at 20:54
  • 1
    Wait! It now shows the entire file after i removed the \n in the printf. That's strange. – Nukodi Sep 05 '16 at 20:56