EOF question...

N

No Such Luck

Hi All:

I have a loop in which I scan an entire file for a particular string
("Error:"). However, I don't know the specifics of testing for the EOF.

while (1) /* This should be while (!EOF) or something */
{
fscanf (file, "%s", string);
if (!strcmp (string, "Error:"));
{
error = 1;
break;
}
}

Of course, if the string "Error:" is not present in the file, this
becomes an infinite loop. What test must be done, and where, to break
out of the loop when the EOF is encountered?

Thanks...

P.S. I checked the FAQ, and only 2 questions/answers were related to
EOFs. Neither seems appropriate for this question.
 
J

jcoffin

No Such Luck wrote:

[ ... ]
while (1) /* This should be while (!EOF) or something */
{
fscanf (file, "%s", string);
if (!strcmp (string, "Error:"));
{
error = 1;
break;
}
}

I would probably make use of the fact that fscanf returns the number of
successful conversions carried out in a call:

while (1==fscanf(file, "%s", string))
 
M

Michael Mair

No said:
Hi All:

I have a loop in which I scan an entire file for a particular string
("Error:"). However, I don't know the specifics of testing for the EOF.

while (1) /* This should be while (!EOF) or something */
{
fscanf (file, "%s", string);
if (!strcmp (string, "Error:"));
{
error = 1;
break;
}
}

Of course, if the string "Error:" is not present in the file, this
becomes an infinite loop. What test must be done, and where, to break
out of the loop when the EOF is encountered?

Thanks...

P.S. I checked the FAQ, and only 2 questions/answers were related to
EOFs. Neither seems appropriate for this question.

Check again. Question 12.2 tells you how to correctly use feof() --
which is essentially to leave it alone and use the value returned
by the functions to read through the file.
Checking the FAQ tells you also to (always) check the return value of
*scanf() which would also helped you

See Jerry's suggestion.

while (fscanf(file, "%s", string) == 1)
{
if (!strcmp (string, "Error:"));
{
error = 1;
break;
}
}

Think about using fgets() or using fscanf() in a safer way, i.e.
without the possibility for an overflow of string.


Cheers
Michael
 
E

Eric Sosman

No said:
Hi All:

I have a loop in which I scan an entire file for a particular string
("Error:"). However, I don't know the specifics of testing for the EOF.

while (1) /* This should be while (!EOF) or something */
{
fscanf (file, "%s", string);
if (!strcmp (string, "Error:"));
{
error = 1;
break;
}
}

Of course, if the string "Error:" is not present in the file, this
becomes an infinite loop. What test must be done, and where, to break
out of the loop when the EOF is encountered?

while (fscanf(file, "%s", string) != EOF) {
if ...
}


Suggestions and alternatives:

- Instead of `!= EOF' you could use `== 1' or `> 0'.

- The unrestricted "%s" in the scanf() family is dangerous;
go back to the FAQ and read Question 12.20.

- fscanf() returns EOF for end-of-file or for I/O error;
you could use the feof() and/or ferror() functions to
tell them apart after the fact.

- (Off-topic, platform-specific) Use "grep" or a relative.
 
N

No Such Luck

Check again. Question 12.2 tells you how to correctly use feof() --
which is essentially to leave it alone and use the value returned
by the functions to read through the file.
Checking the FAQ tells you also to (always) check the return value of
*scanf() which would also helped you

See Jerry's suggestion.

while (fscanf(file, "%s", string) == 1)
{
if (!strcmp (string, "Error:"));
{
error = 1;
break;
}
}

Think about using fgets() or using fscanf() in a safer way, i.e.
without the possibility for an overflow of string.

Thanks for the help. comp.lang.c to the rescue, again.

P.S. Thanks for the semi-colon after the "if" statement. That cost me
about 15 minutes... ;)
 
M

Michael Mair

No said:
Thanks for the help. comp.lang.c to the rescue, again.

P.S. Thanks for the semi-colon after the "if" statement. That cost me
about 15 minutes... ;)

*g* Sorry, I just copied _your_ code... and did (shame on me)
oversee it. Makes you richer in experience.
BTW: splint and Co. can warn about things like this and high
enough warning levels of a good compiler might do so as
well... :)


Cheers
Michael
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top