Advertisement

How exactly does "fscanf" work?

Started by February 23, 2001 07:25 PM
1 comment, last by Tsu 23 years, 11 months ago
If i had a text (or whatever) file:

width = [100]
height = [200]
 
and my program said:
  
fscanf (afile,"height = [%i]\n",theheight);
fscanf (afile,"width = [%i]",thewidth);
/* 
note that width comes first in the file, but height is first to be ''scanned''
*/
  
(I hope I get the right slash on the "\n".. ) Would that work? What i''m trying to ask is, does it ''scan'' the file for the string, or does it behave like fgets (i think thats the one...) and start at the first position and work line by line (and give me an error saying that it can''t find "height = [%1]")... (and for all you who keep telling me to buy a better c book, i''m getting one soon )
Buy a better C book ;-)

That code won''t work, because fscanf will attempt to match whatever string you pass it with whatever it is looking at. It won''t scan the entire file each time. I''m not sure about the \n or indeed the rest of the syntax, since I haven''t used fscanf for well over 5 years, but I''m pretty sure that the code should read

fscanf (afile,"height = [%i]\n",&theHeight);

By the way, re you trying to parse .ini files here?

--
Get a stripper on your desktop!
Advertisement
oops, forgot the "&" before the variable... knew it looked wierd...
that about answered my question... thanks...

parsing .ini files? possibly... i''m just trying to figure out how a load / save map feature will work on a simple little tile engine that i''m working on... i was going to put everything in one big ''configuration''(text) file (by everything, i mean the height, width, scripts to load, positions of things,etc...), inside a datafile with all the other stuff the map needs... i considered using a .ini file, but dont yet know how to use them... i''m assuming differently than a normal text file?

i was hoping it would search the file every time, because i''m making a scripting thing that loads and runs scripts from a file... i dont even remember why i wanted it to search the file... now that i think about it, all i really need it to do at the moment is read it in, one line at a time, and interpret the stuff... but it still would be handy to know... is there a way to search the file? i mean is there an existing function, if not, i can probably figure out how to on my own, but if i dont have to go to the trouble ... i wouldn''t need to search if it was a map file, because it would always be the same format... but for scripting...

hmm... searching a string variable... hmm... i could read in the file one line at a time (using that command that reads a file in one line at a time into a string) and search the string to find out what command to run... yeah...

anyway, i guess i was just wondering how it worked...

This topic is closed to new replies.

Advertisement