Public Group

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

## Recommended Posts

##### Share on other sites
You need to supply scanf funtions with addresses.

sscanf(tempdb,"%d , %s , %s , %d , %d , %d",&items[row].id,&items[row].name,&items[row].type,&items[row].cost,&items[row].str,&items[row].def);

So this should probably do it.

##### Share on other sites
Well, the most obvious error I see that each line in your text file has 7 entries separated by commas, and you 'sscan'ing only 6. That will give unpredictable results in variables.

Second is that you're opening file for binary access (fopen(filename, "r")). To read it in text, use "r+t". That may not be the problem, but just a general correction.

And lastly, use the power of the debugger to step through at least first item and see its values. If they aren't right or what you expect them to be, then you have to look more behind to find the error.

As for sscanf, the correct version would look like:
sscanf(tempdb, "%d,%d,%s,%s,%d,%d,%d", &item_id, &items[row].id, items[row].name, items[row].type, &items[row].cost, &items[row].str, &items[row].def);

Good luck.

##### Share on other sites
How far does your program get before it exits?

If you dont know how to use a debugger, every so often put:
std:::ocut << "got here _SOME_RANDOM_NUMBER" (or whatever passes for that in c....

Keep on doing this until you know exactly where your error is.

##### Share on other sites
You're also incrementing row before your displaying the contents, therefore displaying the contents of the next item you haven't yet read.

##### Share on other sites
For some reason the fgets seems to be the problem..

And none of the solutions worked =\

##### Share on other sites
Check if fopen fails.
You may be using an invalid file descriptor.

##### Share on other sites
I just checked, the file opens properly.. The problem is with the fgets.. as thats where it stops.

EDIT: I just checked to see if it can read the file as is, without decalring the variables and such. For some reason the sscanf messes it up. It reads it, but not all is displayed.

##### Share on other sites
The problem is partially your itemdb.txt file.

1,ShortSword , wep ,100,15,0
2,LongSword , wep ,500,30,0
3,GreatSword , wep ,1000,50,0

works, as sscanf (or all scanf variants?) treat a comma as part of the string, and also breaks a 'string' when a white space is encountered.

Couple this with the revised
sscanf(tempdb,"%d , %s , %s , %d , %d , %d", &items[row].id,items[row].name,items[row].type,&items[row].cost,&items[row].str,&items[row].def);
and it works, at least on my machine.

##### Share on other sites
Okay, i found the problem, and its alot simpler than all the other solutions.

The problem is here:

int row;

I changed it to:

int row=0;

And poof, it works.
The problem was when i set the variable it probably gets set to NULL. You cant have an array which is NULL. And therefore the program crashed =]
Thanks for everyones help anyways.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633270
• Total Posts
3011159
• ### Who's Online (See full list)

There are no registered users currently online

×