Problem reading in Text File
I am reading in a file and storing its information into this structure:
Code:
typedef struct Item
{
char *name;
int area;
struct Item *nextPtr;
} Item;
typedef Item *Ptr;
The text file looks like this:
Code:
Bread
0
Toast
1
Butter
0
...with the name of the item followed by the location of the item in integer form.
I am getting the information from the text file into the structure like so:
Code:
void loadItems(Ptr *sPtr)
{
FILE *file;
char stuff[30];
char area[10];
Ptr newPtr;
if((file = fopen("items.txt", "r")) == NULL)
{
perror("items.txt");
exit(1);
}
while(1)
{
if(fgets(stuff, 30, file) == NULL)
{
break;
}
stuff[strlen(stuff) - 1] = '\0';
newPtr = malloc(sizeof(Ptr));
if(newPtr != NULL)
{
newPtr->name = stuff;
sscanf(area, "%d", &newPtr->area);
}
I didn't include all of the code in this function for simplicity's sake (please let me know if more code would be helpful.
MY QUESTION IS...
When reading in the file, the fgets at the beginning of the while loop still reads in the numbers, even though the sscanf at the end of while loop reads them in as well. Is there any way I can get the fgets to skip the numbers? I figured the sscanf would bump the fgets to the next word, but I guess I was wrong...hopefully that makes sense and isn't too convoluted!