fgets on an empty file

K

Karthick S.

Hi,

fgets returns NULL on error and the first argument on success. I
want to open a file and read it using fgets. This is returning NULL,
when the file is empty. Is there anyway to circumvent this problem
without changing the function call to fscanf or something else?
Thanks in advance.
Rgds,
Karthick S.
 
J

Jens.Toerring

Karthick S. said:
fgets returns NULL on error and the first argument on success. I
want to open a file and read it using fgets. This is returning NULL,
when the file is empty. Is there anyway to circumvent this problem
without changing the function call to fscanf or something else?

What's the problem? If the file is empty you hit EOF immediately
and fgets() returns NULL (not only on error). If there's something
in the file fgets() also will return NULL, but only later after you
have read everything from the file. So, you must always be prepared
to get NULL when you use fgets(), empty or non-empty file.

Regards, Jens
 
B

Barry Schwarz

Hi,

fgets returns NULL on error and the first argument on success. I
want to open a file and read it using fgets. This is returning NULL,
when the file is empty. Is there anyway to circumvent this problem
without changing the function call to fscanf or something else?

Whenever fgets returns NULL, you should use feof() to determine if it
was an error or an end of data condition.


<<Remove the del for email>>
 
K

Karthick S.

What's the problem? If the file is empty you hit EOF immediately
and fgets() returns NULL (not only on error). If there's something
in the file fgets() also will return NULL, but only later after you
have read everything from the file. So, you must always be prepared
to get NULL when you use fgets(), empty or non-empty file.

Regards, Jens

Hi,

I am attaching a code snippet:

/*****************************Code Begins*********************/
int ReadFile(char **r_pstrState)
{
int iRetVal = SUCCESS;
FILE *fp = (FILE*) NULL;
char strErrMesg[1024];

fp = fopen(MYFILE, "r");
if(NULL == fp)
{
sprintf(strErrMesg, "Opening the file %s failed. Error: %s",
CURRSTATECONF, strerror(errno));
fputs(strErrMesg, stderr);
fputs("\n", stderr);
iRetVal = FAILURE;
return(iRetVal);
}
errno = 0;
if(fgets(*r_pstrState, 5, fp) == NULL)
{
sprintf(strErrMesg, "Reading from the file %s failed. Error: %s",
CURRSTATECONF, strerror(errno));
fputs(strErrMesg, stderr);
fputs("\n", stderr);
iRetVal = FAILURE;
return(iRetVal);
}
iRetVal = SUCCESS;
return(iRetVal);
}

int main(void)
{
char strArr[6];

return(ReadFile(&strArr));
}
/*****************************Code Ends***********************/

Now if the file that I had #defined as MYFILE is blank, then I get
the error message:

Reading from the file <path to file> failed. Error: Success.

I would like to know the reason for this.

Thank you.
Rgds,
Karthick S.
 
C

Chris Torek

if(fgets(*r_pstrState, 5, fp) == NULL) {
sprintf(strErrMesg,
"Reading from the file %s failed. Error: %s",
CURRSTATECONF, strerror(errno));[/QUOTE]

This is not a good idea. The fgets() function is never guaranteed
to set errno to anything, even on error. In practice, on real
operating systems, reading from a file does tend to set errno on
error, but not on EOF.
[at EOF] I get the error message:
Reading from the file <path to file> failed. Error: Success.

This is quite unsurprising.

Do not inspect errno after fgets() fails. If you want to know
*why* fgets() has failed, use feof() and ferror() to distinguish
between "fgets() failed because of end-of-file" and "fgets() failed
because of error reading file".

Note that you should not use feof() *before* getting an input
failure of some sort, because feof() does not predict input failure
due to end of file, but rather "postdict" it: *after* failure, one
of feof() or ferror() should be true.

Note again that Standard C does not guarantee anything useful
in errno even in the ferror() case, so something like:

if (fgets(*r_pstrState, 5, fp) == NULL) {
if (feof(fp)) {
... normal end of file case ...
} else {
sprintf(strErrMesg,
"Reading from the file %s failed. The system "
"most recently reported \"%s\", which may or "
"may not have anything to do with the failure.",
CURRSTATECONF, strerror(errno));
...
}
}

will include the (potential) errno-based information along with a
caveat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

understanding fgets() 11
Having an issue with empty arrays 2
HELP:function at c returning (null) 4
fgets problem 23
question on fgets 13
fgets 6
Replacing fgets 32
fgets() replacement 20

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top