When reading characters from a file using fgetc, it seems to read an extra newline character at the end of the file even when a text editor (GVim) doesn't show one there. This happens on a Unix machine, but I don't have the problem when I run it in Windows. Anybody know why?
Here's the code:
Code:int currChar = 'a'; char buffer[256]; FILE *input; // ... // Process each line of the input file while (currChar != EOF) { int i; // Read line for (i = 0; (currChar = fgetc(input)) != '\n' && currChar != EOF; i++) { buffer[i] = currChar; } buffer[i] = '\0'; // Output each line printf("Buffer (%i): %s\n", i, buffer); }
The input text file:
Code:abc x y z
The Unix output:
Code:Buffer (3): abc Buffer (1): x Buffer (1): y Buffer (1): z Buffer (0):
The Windows output:
Code:Buffer (3): abc Buffer (1): x Buffer (1): y Buffer (1): z



LinkBack URL
About LinkBacks



