Simple Function to input from a file.

Posted by Azrith on Fri 21 Feb 2003 02:45 AM — 4 posts, 15,122 views.

#0
Hey everyone. Im back! hehe I am trying to set up a simple array (or not so simple for me apparantly). I want to input a bunch of 0s and 1s from a text file into this integer array. I also only want it to input 1 integer at a time.
For some reason I am having trouble. The main input loop is
for (dumcount = 0 ;dumcount < 200 && (!feof(fpList)) ; dumcount++ )
{
dummynum = fgetc(fpList);
vnum_ship_list[dumcount] = dummynum;
}
I believe the problem lies with the EOF but I can't seem to figure out what it is.
Any help would be greatly appreciated!
Thanks!
Australia Forum Administrator #1
In what way does it not work? For a start, you said an "integer array" however you will be reading in "0" or "1" not 0 or 1.

In other words, "0" will be 48 decimal (0x30 in hex), and "1" will be 49 decimal (0x31 in hex), so in neither case will you get a real zero into the array.

One variation would be to change the line:


vnum_ship_list[dumcount] = dummynum;	


to read:


if (dummynum == "1")
  vnum_ship_list[dumcount] = 1;
else
  vnum_ship_list[dumcount] = 0;

Amended on Fri 21 Feb 2003 09:29 PM by Nick Gammon
#2
Ahh I see what you're saying about the input. I thought fgetc performed differently but I believe my real problem is the EOF flag in my for loop. I initially set up this array to be all 0s. Then I call this load function. For some reason even if I have a blank file it still goes into this for loop. I can't seem to figure out why it is entering the for loop when the file is blank.:) Thanks for the help!
#3
Wow very interesting. After I changed the code you had told me it appears to be working fine now. I am working on revamping smaug 1.4 to a new scifi mud. :) In the middle of creating ships. I didn't like the way star wars was using them so I figured Id start fresh hehe.
Thanks for the quick help Nick!