Hi! I've been having problems regarding my file reading. I've noticed that it returns a weird value, (it returns a 0.00) and stores this incorrect value in the structure.I've noticed that this happens usually if the file is empty. Here's the part of my code that deals with the reading of the file. I've initialized the fp in another part of the program, by the way.
I'd aprreciate all your help
Code:int reads () { int i= 0, j; char c; fp = fopen("dishes.txt", "r"); if (fp != NULL) { while (!feof(fp)) { fscanf (fp,"%50s %50s %f", dish[i].name, dish[i].describe, &dish[i].price); dishtotal++; i++; }//while loop }//if // printf ("%d", i); fclose(fp); getch(); return; }
The format of the file looks like this:
string1 string2 price(in float)
sundae chocolate 5.00
lasagna italian 50.00
and so on...
Last edited by ZekeDragon; 03-21-2010 at 01:39 AM. Reason: Please use [code] tags (the # button) when posting code.
Could you give us an example file that you open which causes the problem? You said it usually happens when the file is empty... which really shouldn't be that surprising.![]()
Wow I changed my sig!
Say I have a file that contains these:
Lasagna Italian Dish 10.00
Sundae Chocolate 9.00
Nachos Mexican 5.00
My program has a delete function which deletes the items in the file, so there are really instances that the file must be empty. I don't want it to return weird values because it kinda mucks up the program , so there![]()
Maybe this will work:
If it returns the weird value, say the file is empty and stop the program.Code:if (dish[i].price==0) { printf("File is empty!\n"); fclose(fp); return 1; }
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Avoid loop control using EOF. Instead, check the return value of fscanf for correctness.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks