I need to count the number of "timesteps" in a file (format shown below) I cound this by counting the number of lines that have 2 integers and a single double and nothing else in them using the code
Code:
int countTimeSteps(FILE *position)
{
char line [MAXNUM]; //lines will be much less than 1000 characters, but it can't hurt
int check,x,y,N_timeSteps;
double z;
N_timeSteps = 0;
while (fgets(line, MAXNUM, position) != NULL) //read in file 1 line at a time
{
//puts(line);
check = sscanf(line,"%d%d%lf",&x,&y,&z);
if (check != 0 && check != EOF) //if the line contains an integer value at the start
{
++N_timeSteps;
}
}
return N_timeSteps;
}
However, this seems to match every line.... Why is this not working?
Here is part of the file I am reading from:
Code:
5 5 0
1 0 0 0 0 0 0
1 5.7993 5.7993 0 0 0 0
1 5.7993 0 -5.7993 0 0 0
1 5.7993 0 5.7993 0 0 0
1 5.7993 -5.7993 0 0 0 0
-1 2.0504 0 0 0 0 0
-1 7.8496 5.7993 0 0 0 0
-1 7.8496 0 -5.7993 0 0 0
-1 7.8496 0 5.7993 0 0 0
-1 7.8496 -5.7993 0 0 0 0
5 5 0.12
1 2.9581e-06 0 0 1.1595e-06 0 0
1 5.7993 5.7993 0 1.4193e-06 -6.9289e-08 0
1 5.7993 -1.3264e-23 -5.7993 1.4193e-06 -3.2084e-24 6.9289e-08
1 5.7993 -1.3264e-23 5.7993 1.4193e-06 -3.2084e-24 -6.9289e-08
1 5.7993 -5.7993 0 1.4193e-06 6.9289e-08 0
-1 1.2922 0 0 -0.29463 0 0
-1 6.9823 5.8662 0 -0.33479 0.021945 0
-1 6.9823 0 -5.8662 -0.33479 0 -0.021945
-1 6.9823 0 5.8662 -0.33479 0 0.021945
-1 6.9823 -5.8662 0 -0.33479 -0.021945 0
5 5 0.23
1 9.5596e-06 0 -1.7237e-23 1.5282e-06 0 -1.6028e-23
1 5.7993 5.7993 -2.6508e-24 1.6843e-06 1.5394e-08 -6.4124e-24
1 5.7993 -3.117e-23 -5.7993 1.6843e-06 -1.3807e-40 -1.5394e-08
1 5.7993 -3.117e-23 5.7993 1.6843e-06 -1.3807e-40 1.5394e-08
1 5.7993 -5.7993 -2.5042e-24 1.6843e-06 -1.5394e-08 -6.0848e-24
-1 -0.37038 0 1.9004e-18 -0.38423 0 1.5304e-18
-1 5.1619 5.9365 2.5342e-18 -0.39775 0.0021853 2.6822e-18
-1 5.1619 0 -5.9365 -0.39775 0 -0.0021853
-1 5.1619 0 5.9365 -0.39775 0 0.0021853
-1 5.1619 -5.9365 2.5358e-18 -0.39775 -0.0021853 2.6835e-18
The function should return 3, but it is returning the number of lines instead. Why is this happening? I can see why it is matching the second line, perhaps, but not all of them...
So: Is there a function that I can se to check if a line contains ONLY
"[integer] [integer] [double]"? and nothing else?