|
warning: ignoring return value of 'int fscanf
|
Reply to this subject
Start a new subject
 
Refresh page
| Posted by |
Robert Powell
Australia (349 posts) bio
|
| Date |
Mon 26 Oct 2009 03:02 AM (UTC) [ quote
] |
| 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);
|
EldhaMUD Game Developments
The_Fury: Lead Developer, Head Coder
http://fury.eldhamud2.org
| top |
|
| Posted by |
David Haley
USA (3,881 posts) bio
Moderator |
| Date |
Reply #1 on Mon 26 Oct 2009 04:28 AM (UTC) [ quote
] |
| 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 (349 posts) bio
|
| Date |
Reply #2 on Mon 26 Oct 2009 07:17 AM (UTC) [ quote
] |
| 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. |
EldhaMUD Game Developments
The_Fury: Lead Developer, Head Coder
http://fury.eldhamud2.org
| top |
|
| Posted by |
Worstje
Netherlands (867 posts) bio
|
| Date |
Reply #3 on Mon 26 Oct 2009 07:57 AM (UTC) [ quote
] |
| 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 (349 posts) bio
|
| Date |
Reply #4 on Mon 26 Oct 2009 08:00 AM (UTC) [ quote
] 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. |
EldhaMUD Game Developments
The_Fury: Lead Developer, Head Coder
http://fury.eldhamud2.org
| top |
|
| Posted by |
Heaven32
(1 post) bio
|
| Date |
Reply #5 on Sat 02 Oct 2010 07:27 AM (UTC) [ quote
] |
| 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.
7,403 views.
Reply to this subject
Start a new subject
 
Refresh page
top
Comments to:
Gammon Software support
Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )