Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ ROM ➜ Compiling the server ➜ warning: ignoring return value of 'int fscanf

warning: ignoring return value of 'int fscanf

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Robert Powell   Australia  (367 posts)  Bio
Date Mon 26 Oct 2009 03:02 AM (UTC)
Message

act_info.c:2562: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result


Ok, this is the next warning that i do not understand, could someone explain whats going on here. Google just returns hundreds of bug pages.



fscanf(fp,"%s",nameread);

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Mon 26 Oct 2009 04:28 AM (UTC)
Message
It means that you shouldn't ignore the return value of fscanf, because it can give you useful/important information. What you do with that information depends on the context in which you're reading from this file, and what an error means.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Robert Powell   Australia  (367 posts)  Bio
Date Reply #2 on Mon 26 Oct 2009 07:17 AM (UTC)
Message
Ok, now i understand why its throwing the error, so how do i go about using the return value to make the compiler happy.


fscanf(fp,"%d %s %s\n",&desc,name,host);

if (desc == -1 || feof( fp ))
	break;


All of the code examples i have looked at dont seem to be doing anything different to me. This is the reference i have used http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/


Thanks for your help David, what works in C does not always work right in C++ and for noobs like myself its all a trifle confusing.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #3 on Mon 26 Oct 2009 07:57 AM (UTC)
Message
From the same page you linked:

Quote:
Return Value
On success, the function returns the number of items succesfully read. This count can match the expected number of readings or be less -even zero- in the case of a matching failure.
In the case of an input failure before any data could be successfully read, EOF is returned.


So you will need to catch the returnvalue (like in retvalue = fscanf(...);) and test if the values are correct.

The example they give is minimal, and works if you get correct input. It will have unexpected results if the data you are reading is not formatted correctly, and it is exactly those situations which you need to handle - hence the existence of a warning.
Top

Posted by Robert Powell   Australia  (367 posts)  Bio
Date Reply #4 on Mon 26 Oct 2009 08:00 AM (UTC)

Amended on Mon 26 Oct 2009 08:01 AM (UTC) by Robert Powell

Message
Would this be closer to the mark ? sure i now this wont compile, but is that the correct way to use the return value?


ret = fscanf(fp,"%d %s %s\n",&desc,name,host);

if (ret == EOF))
	break;



HEHE posted this as Worstje did, i kind of worked it out eventually. Thanks.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by Heaven32   (1 post)  Bio
Date Reply #5 on Sat 02 Oct 2010 07:27 AM (UTC)
Message
The warnings are trying to tell you that you are ignoring the error status of those two calls, i.e. you aren't checking if they succeeded.

The best solution is to check the return values like you should be doing.

Heaven
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


34,867 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.