\377 character in c

saidozcan picture saidozcan · Dec 12, 2012 · Viewed 21.3k times · Source

i am trying to read a file in c. i have a .txt file and it has that content:

file_one.txt file_two.txt file_three.txt file_four.txt

when i try to read this file with fopen i get this output:

file_one.txt file_two.txt file_three.txt file_four.txt\377

what does \377 mean? Here's my code.

    #include <stdio.h>

    #include <stdlib.h>

    int main(int argc, const char * argv[]){

        FILE *filelist;

        char ch;

        filelist=fopen("file-path", "rt");

        while (!feof(filelist)) {
            ch = getc(filelist);
            printf("%c",ch);
        }

        fclose(filelist);

        return 0;
    }

Answer

Daniel Fischer picture Daniel Fischer · Dec 12, 2012

The \377 is an octal escape sequence, decimal 255, all bits set. It comes from converting EOF - which usually has the value -1 - to a char, due to

while (!feof(filelist)) {

feof(filelist) only becoming true after you have tried to read past the file.

So at the end of the file, you enter the loop once more, and the getc() returns EOF.