I'm having trouble reading a data file's data using sscanf. The format of the data file (called file.txt) looks like this:
NUMBER OF PARAMS 249
TITLE BLOCK
B211W111
444-565AG
FIGURE 3.6-1a
3-46
NORMAL RUN
COWL 20, NORMAL MODE
CONDITION 6.18-1
PARAMS
time 10 90 8.0 3.0 6.0
speed 10 190 9.0 3.0 6.0
height -100 350 9.0 15.0 6.0
time 10 90 8.0 3.0 1.0
distance0 200000 4.0 3.0 1.0
time 10 90 8.0 3.0 1.0
distance 0 200000 4.0 3.0 1.0
My code should skip down to the "PARAMS" section (which it does) and then it is supposed to read the first variable name and store it (in junk), then read the next two fields (min and max) following the variable name. The problem is that it is not storing the min and max correctly.
Thanks in advance!
My code looks like this:
Code:
#include <stdio.h>
#include <string.h>
int main()
{
FILE *f;
char junk[20][10], vars[20][10], min[20][10], max[20][10];
char * pch, * pch1, s[500];
int i = 0;
f=fopen("file.txt","r");
if (!f)
return 1;
//find the first variable for min and max data
while (fgets(s,sizeof(s),f)!=NULL)
{
// skip down to the "PARAMS" line
pch = strstr (s,"PARAMS");
if(pch != NULL)
{
// find each variable
while (fgets(s,sizeof(s),f)!=NULL)
{
pch1 = strstr (s,vars[i]);
if(pch1 != NULL)
{
// record each variables min and max data
printf("variable name = %s\n",vars[i]);
sscanf( s, "%s %s %s", junk[i], min[i], max[i]);
printf("min = %s\n",min[i]);
i++;
}
}
}
}
fclose(f);
return 0;
}