I have the following program within main
fgets(buffer, 99, stdin);
while (!feof(stdin))
{
printf("PrintF : %s\n", buffer);
fgets(buffer, 99, stdin);
}
return 0;
I start a cmd window and compile, then run the program with a file argument.
file.exe < samplefile.txt
within the txt file I have the following lines
Hello this is
a sample file
by alanz2223
however the output is
PrintF : Hello this is
PrintF : a sample file
it seems to omit the "by alanz2223" line. according to the fgets() function description it reads characters from stream and stores them as a C string into str(the first parameter) until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
according to this description then a newline character was approached after "Hello this is " and the output should of ended there but it goes onto the next line and outputs "a sampe file " and then approaches a newline character but there is a next line. It seems that after the second line the program terminates as if it approached the end of the file which is not the case.