I have just recently started working with I/O
in C
. Here is my question -
I have a file, from which I read my input. Then I use fgets()
to get strings in a buffer which I utilise in some way. Now, what happens if the input is too short for the buffer i.e. if the first read by fgets()
reaches EOF
. Should fgets()
return NULL
(as I have read in fgets()
documentation)? It seems that it doesn't and I get my input properly. Besides even my feof(input)
does not say that we have reached EOF
.
Here is my code snippet.
char buf[BUFSIZ];
FILE *input,
*output;
input = fopen(argv[--argc], "r");
output = fopen(argv[--argc], "w");
/**
* If either of the input or output were unable to be opened
* we exit
*/
if (input == NULL) {
fprintf(stdout, "Failed to open file - %s.\n", argv[argc + 1]);
exit(EXIT_FAILURE);
}
if (output == NULL) {
fprintf(stdout, "Failed to open file - %s.\n", argv[argc + 0]);
exit(EXIT_FAILURE);
}
if (fgets(buf, sizeof(buf), input) != NULL) {
....
}
/**
* After the fgets() condition exits it is because, either -
* 1) The EOF was reached.
* 2) There is a read error.
*/
if (feof(input)) {
fprintf(stdout, "Reached EOF.\n");
}
else if (ferror(input)) {
fprintf(stdout, "Error while reading the file.\n");
}
RETURN VALUE Upon successful completion, fgets() returns s. If the stream is at end-of-file, the end-of-file indicator for the stream is set and fgets() returns a null pointer. If a read error occurs, the error indicator for the stream is set, fgets() returns a null pointer and sets errno to indicate the error.
fgets() function in C The function reads a text line or a string from the specified file or console. And then stores it to the respective string variable. Similar to the gets() function, fgets also terminates reading whenever it encounters a newline character.
Return Value The fputs() function returns EOF if an error occurs; otherwise, it returns a non-negative value. The fputs() function is not supported for files that are opened with type=record.
fgets() won't add a newline; it will include the newline that it read that marks the end of line. That way, you can tell whether you read the whole line or not.
The documentation for fgets()
does not say what you think it does:
From my manpage
fgets()
reads in at most one less thansize
characters from stream and stores them into the buffer pointed to bys
. Reading stops after anEOF
or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0
') is stored after the last character in the buffer.
And later
gets()
andfgets()
returns
on success, andNULL
on error or when end of file occurs while no characters have been read.
I don't read that as saying an EOF
will be treated as an error condition and return NULL
. Indeed it says a NULL
would only occur where EOF
occurs when no characters have been read.
The POSIX standard (which defers to the less accessible C standard) is here: http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html and states:
Upon successful completion,
fgets()
shall returns
. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set andfgets()
shall return a null pointer. If a read error occurs, the error indicator for the stream shall be set,fgets()
shall return a null pointer, and shall seterrno
to indicate the error.
This clearly indicates it's only going to return a NULL
if it's actually at EOF
when called, i.e. if any bytes are read, it won't return NULL
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With