By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,686 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,686 IT Pros & Developers. It's quick & easy.

EOF question...

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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))

--
Later,
Jerry.

The universe is a figment of its own imagination.

Nov 14 '05 #2

P: n/a


No Such Luck wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #3

P: n/a
No Such Luck wrote:
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-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #4

P: n/a
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... ;)

Nov 14 '05 #5

P: n/a
No Such Luck wrote:
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... ;)


*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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.