0

I'm comparing my output with standard outputs, when I run

diff sampleOutput testOutput

It gives me:

< this has [sic] no [sic] last [sic] linefeed [sic]
\ No newline at end of file
---
> this has [sic] no [sic] last [sic] linefeed [sic]

cmp tells me it's because there is an EOF on sampleOutput:

cmp: EOF on sampleOutput after byte 361, in line 9

Is there any way I can make this go away?

Update: The last word is printed in this way:

strcat(word, " [sic]");
printf("%s", word);
Bubblethan
  • 379
  • 3
  • 11
  • 1
    Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) Quite often, the last line of a file does not have a newline. `fgets` retains the newline at the end of each line, which you can remove. – Weather Vane May 30 '20 at 07:14
  • 1
    "I'm comparing my output". Output of what? Please show your code. We can't really give a very good answer if we don't know exactly what your code does and how it does it. – kaylum May 30 '20 at 07:16
  • If you miss the newline `printf("\n");`. If you do not want it, look for that `"\n"` in your code and remove it. – Yunnosch May 30 '20 at 07:17
  • 7
    Note that `EOF` is not an actual character in the file. It is the return value of some functions that read from file. So the issue is not that your file is missing an `EOF` it is that it has an extra newline. Which is what some of the other comments are trying to help with. But to give precise help we would need to see the code. – kaylum May 30 '20 at 07:23
  • Just fix your files. The last line should end with a newline, just as every other line does. If it doesn't, then the last line is abnormal and prone to problems like this. – Tom Karzes May 30 '20 at 07:28
  • 2
    @kaylum some older MS DOS text files do have an actual EOF marker `0x1A` (hence the Ctrl-Z typed to end the `stdin` stream). This is one reason why Windows makes a distinction between text files and binary files. – Weather Vane May 30 '20 at 07:36
  • 1
    @WeatherVane I haven't seen such a file in the last 30 years ;-) – Jabberwocky May 30 '20 at 08:16
  • 1
    @Jabberwocky some are still using 30 year old tools and systems :) – Weather Vane May 30 '20 at 08:17
  • If the last line is printed with the code you show, then there is no line feed (`\n`) at the end of this last line. Open both files you mentioned with a text editor and move the cursor to the very end of the file, you'll notice the difference. – Jabberwocky May 30 '20 at 08:19
  • @WeatherVane This behavior is copied from CP/M because its file system did not store the exact file size, just multiples of 128 bytes. You needed the EOF to know where a (text) file ends. In MS DOS it is not really necessary, but those tools kept it. – the busybee May 30 '20 at 16:10
  • @thebusybee even today, an MS VC executable will stop reading a text file if it encounters `0x1A`. – Weather Vane May 30 '20 at 16:28

0 Answers0