[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  ROM
. -> [Folder]  Compiling the server
. . -> [Subject]  warning: ignoring return value of 'int fscanf
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

warning: ignoring return value of 'int fscanf

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


Posted by Robert Powell   Australia  (367 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Robert Powell   Australia  (367 posts)  [Biography] 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.
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] 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.
[Go to top] top

Posted by Robert Powell   Australia  (367 posts)  [Biography] 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.
[Go to top] top

Posted by Heaven32   (1 post)  [Biography] 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
[Go to top] 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.


14,488 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]