Jump to content
  • Advertisement
Sign in to follow this  
_Zido_

File reading problem.. Please help.

This topic is 4837 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

Ok, I'm making a test-based rpg game, and right now i made a little program to test if my item reading "system" works. What it does is reads from a file, "itemsdb.txt", the item information, and then displays the data for each item loaded. The items data is seperated by commas, for example: data1,data2,data3..ect... The program compiles with no problems, although when i run the program it says: "testitems.exe has encoundered a problem and needs to close". This is my sourcecode: #include <stdio.h> #include <stdlib.h> typedef struct { int id; char name[75]; char type[25]; int cost; int str; int def; }items_db; items_db items[50]; int main() { FILE* itdb; char tempdb[255]; int row; itdb = fopen("itemdb.txt","r"); while(fgets(tempdb,255,itdb)!=NULL) { 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); row++; printf("==============================\n"); printf("| ITEM ID %d |\n",items[row].id); printf("==============================\n"); printf("Item name = %s...loaded.\n",items[row].name); printf("Item type = %s...loaded.\n",items[row].type); printf("Item cost = %d...loaded.\n",items[row].cost); printf("Item str = %d...loaded.\n",items[row].str); printf("Item def = %d...loaded.\n",items[row].def); printf("Item id %d successfully finished loading.\n",items[row].id); } fclose(itdb); system("pause"); return 0; } //end of file The itemdb.txt contains: 1,Short Sword,wep,100,15,0 2,Long Sword,wep,500,30,0 3,Great Sword,wep,1000,50,0 Thanks for any help

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!