Advertisement Jump to content
Sign in to follow this  

reading in arrays with fscanf

This topic is 3695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm having a really basic issue with fscanf but I'm not a very good programmer in that I don't really know the fundamentals of C++. Basically I have a File that is a few hundred lines long and each line has the format "long bool 32bithex" so I have 3 arrays, one to store the data from each of these 3 columns. The code snippet is as follows (the file is open):
long times0[1000];
long addr0[1000];
bool rw0[1000];
FILE *f0;

for(int i=0;i<100;i++)
     fscanf(f0, "%li %b %lx", ×0, &rw0, &addr0);
     printf("TIMES:  %i %li\n", i, times0);
However, using this code, all 3 columns get read into the times0 array! times0[0] has the right value, but times0[1] somehow has what rw0[0] should have had and times0[2] has what addr0[0] should have had! What am I doing wrong here? Edit: Alternatively, if my code is just acting wacky and is beyond repair, could someone suggest how to do what I want to do properly? [Edited by - innocuous on December 4, 2008 3:22:19 AM]

Share this post

Link to post
Share on other sites
print the return value of fscanf, it tells you how many arguments were scanned successfully.

Show us some example lines from your file.

Share this post

Link to post
Share on other sites
Thanks for the reply. The file reads as follows:

183 0 0x00000010
269 1 0x094055b0
346 1 0x094055a8
361 1 0x094055ac

...and so on. When I save the return value of fscanf to a variable and output it, it appears as if fscanf is only scanning 1 value at a time although I am telling it to scan 3. I used the code

int temp = fscanf(f0, "%li %b %lx", ×0, &rw0, &addr0);
printf("ADDR: %i %li %i\n", i, times0, temp);

The result looks like:

ADDR:  0 183 1
ADDR: 1 0 1
ADDR: 2 16 1
ADDR: 3 269 1
ADDR: 4 1 1

... and so on. It's definitely only scanning one variable at a time, but I don't know how to make it scan more than one now as I thought the code I used was correct.

Share this post

Link to post
Share on other sites
As far as I can tell, the format string %b does not exist. This is no surprise because you are using the C function scanf, and there is no boolean data type in C. Try reading into a temporary int and then assign that int to the bool in the array. Or use proper C++ I/O.

Share this post

Link to post
Share on other sites
That was 100% the problem, I have no idea where I got the idea that there was a binary read in format. Thanks, I wouldn't have caught that for awhile!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!