Jake Cooper
Jake Cooper

Reputation: 109

fscanf reading everything except for first line

int k;
float regionID;
int t;
char string[100];
float avgTemp,totalTemp;
for(k = 0; k < MAX_STATIONS; k++){
    if (fgets(string, sizeof(string), fp) == 0){
        break;
    }
    fscanf(fp,"%d %f %d %d %d %d %d %f %f %f", &stationInfo[k].stationID, &stationInfo[k].temperature, &stationInfo[k].year, &stationInfo[k].month, &stationInfo[k].day, &stationInfo[k].hour, &stationInfo[k].minute, &stationInfo[k].location.latitude, &stationInfo[k].location.longitude, &regionID);
    printf("%d %1.2f %d %d %d %d %d %f %f\n", stationInfo[k].stationID, stationInfo[k].temperature, stationInfo[k].year, stationInfo[k].month, stationInfo[k].day, stationInfo[k].hour, stationInfo[k].minute, stationInfo[k].location.latitude, stationInfo[k].location.longitude);

}

So, my program reads my file almost perfectly, but it will not read the first line of my file. Anyone know what might be causing this problem and how I might be able to fix it?

Upvotes: 1

Views: 702

Answers (2)

John Odom
John Odom

Reputation: 1223

The reason why your program is "not reading the first line of the file" is because it actually already read the first line when you called

if (fgets(string, sizeof(string), fp) == 0)

The change to fix this is simple:

for(k = 0; k < MAX_STATIONS; k++)
{
    if (fscanf(fp,"%d %f %d %d %d %d %d %f %f %f", &stationInfo[k].stationID,
                                                   &stationInfo[k].temperature,
                                                   &stationInfo[k].year,
                                                   &stationInfo[k].month,
                                                   &stationInfo[k].day,
                                                   &stationInfo[k].hour,
                                                   &stationInfo[k].minute,
                                                   &stationInfo[k].location.latitude,
                                                   &stationInfo[k].location.longitude,
                                                   &regionID) > 10)
    {
        break;
    }
    printf("%d %1.2f %d %d %d %d %d %f %f\n", stationInfo[k].stationID,
                                              stationInfo[k].temperature,
                                              stationInfo[k].year,
                                              stationInfo[k].month,
                                              stationInfo[k].day,
                                              stationInfo[k].hour,
                                              stationInfo[k].minute,
                                              stationInfo[k].location.latitude,
                                              stationInfo[k].location.longitude);
}

Note the fscanf's return value:

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

Hope this is what you are looking for!

Upvotes: 1

paddy
paddy

Reputation: 63451

You are quite intentionally reading and discarding a line here:

if (fgets(string, sizeof(string), fp) == 0) {

I think you meant to use sscanf instead of fscanf, so that you use the data returned to you by fgets.

sscanf(string,"%d %f %d %d %d %d %d %f %f %f", ... );

Upvotes: 4

Related Questions