So I'm using struct to create an address that looks like this:
typedef struct Address{
char street[100];
char city[100];
char state[3];
int zip;
}address;
I have a txt file that looks like this:
123 Anywhere
Spokane
WA
99223
500 Sprague Ave
Spokane
WA
99201
319F CEB
Cheney
WA
99004
When I read in the file I get a huge problem. It will read in a couple of lines correctly but most everything else is wrong. Any numbers are completely wrong.
int fillArray(address *array, FILE * fin){
int i=0, total=0;
do{
fgets(array[i].street, 100, fin);
fgets(array[i].city, 100, fin);
fgets(array[i].state, 3, fin);
fgets(array[i].street, 100, fin);
fscanf(fin, "%d", &array[i].zip);
total++;
i++;
}while(!feof(fin));
return total;
}
Obviously I'm doing something wrong but I'm not sure what I'm doing wrong. I thought that if I use fgets, it will get the line and then store it into the area that I needed it to. I had used fscanf for everything before hand and I didn't get anywhere either. It would just get the 123 and then Anywhere into the next places since it goes until the first whitespace it sees. Is there a way to use fscanf to get the entire line, or isn't that what fgets does? Is there a simpler way of doing this? Yes I know that I shouldn't use while(!feof(fin)), but our teacher is teaching that right now and that is what I'm going to be using for the time being. Thank you in advance. I have been searching around online and can't seem to get where I need to go.