L
learner
I have datafiles like this:
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1
0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2
0 1941 0.00 0.00 0.00 0.00 0.52 0.00 0.00 0.17 1.07 0.09 3
0 1941 0.04 0.00 0.00 0.00 0.00 0.62 0.00 0.01 0.00 0.00 4
0 1941 0.00 0.02 0.00 0.00 0.00 0.22 0.00 0.00 0.00 0.16 5
0 1941 0.00 0.00 0.00 0.00 0.09 0.04 0.00 0.00 0.00 0.00 6
0 1941 0.00 0.83 0.00 0.00 0.00 0.00 0.00 0.00 0.22 0.03 7
0 1941 0.31 0.00 0.00 0.00 0.00 0.00 0.03 0.00 0.00 0.00 8
0 1941 0.00 0.48 0.38 0.07 0.00 0.12 0.00 0.00 0.00 0.00 9
0 1941 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.37 0.00 0.00 10
0 1941 1.13 0.01 0.00 0.00 0.00 0.00 0.00 0.07 0.00 0.00 11
0 1941 0.00 0.16 0.00 0.00 0.00 0.23 0.00 0.00 0.00 0.00 12
0 1941 0.01 0.10 0.00 0.00 0.00 0.00 0.00 0.00 0.13 0.00 13
0 1941 0.00 1.18 0.38 0.73 0.00 0.16 0.00 0.00 0.00 0.00 14
0 1941 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.30 0.00 0.00 15
0 1941 0.56 0.02 0.00 0.00 0.06 0.00 0.31 0.00 0.00 0.00 16
0 1941 0.00 0.00 0.00 0.34 0.29 0.32 0.00 0.00 0.00 0.00 17
0 1941 0.00 0.00 0.00 1.06 0.67 0.00 0.00 0.00 0.00 1.41 18
0 1941 0.00 0.00 0.00 0.00 0.52 0.00 0.00 0.00 0.00 0.29 19
0 1941 0.00 0.20 0.44 0.01 0.00 0.00 0.00 0.05 0.32 0.00 20
0 1941 0.00 0.00 0.00 0.00 0.75 0.00 0.00 1.10 0.00 1.87 21
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.00 22
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.28 0.78 0.00 0.00 23
0 1941 0.00 0.35 0.00 0.32 0.00 0.00 0.50 0.00 0.00 0.00 24
0 1941 0.00 0.00 0.00 0.00 1.38 0.25 1.16 0.00 0.00 0.00 25
0 1941 0.32 0.00 0.00 0.00 0.00 0.25 0.04 0.00 0.00 0.00 26
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 27
0 1941 0.16 0.22 0.00 0.00 0.16 0.00 0.00 0.00 0.00 0.00 28
0 1941 0.00 0.00 1.24 0.00 0.10 0.07 0.02 0.02 0.02 0.00 29
0 1941 0.25 0.04 0.56 0.27 0.00 0.00 0.00 0.00 0.00 0.00 30
0 1941 0.00 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.01 31
0 1941 0.51 0.01 0.46 0.00 0.00 0.92 0.00 0.00 0.00 0.00 32
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.24 0.00 0.00 33
0 1941 0.00 0.00 0.43 0.19 0.00 0.00 0.00 0.00 0.00 0.00 34
0 1941 0.00 0.00 0.05 0.12 0.38 0.26 0.01 0.00 0.00 0.00 35
0 1941 0.00 0.01 0.71 0.00 0.00 0.20 0.13 0.62 0.90 0.00 36
0 1941 0.00 0.01 0.02 0.04 0.00 0.00 37
0 1942 0.00 0.00 0.00 0.00 0.02 0.03 0.00 0.00 0.00 0.00 1
0 1942 0.00 0.02 0.00 0.00 0.08 0.00 0.00 0.00 0.00 0.00 2
0 1942 0.00 0.00 0.00 0.00 0.10 0.00 0.00 0.83 0.90 0.18 3
0 1942 0.00 0.00 0.00 0.00 0.31 0.00 0.00 0.00 0.00 0.00 4
0 1942 0.31 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 5
0 1942 0.00 0.00 0.00 0.58 0.00 0.00 0.00 0.00 0.12 0.00 6
0 1942 0.00 0.00 0.00 0.00 0.04 0.31 0.00 0.00 0.00 0.00 7
0 1942 0.00 0.13 0.03 0.17 0.37 0.00 0.00 0.00 0.00 0.00 8
0 1942 0.00 1.03 0.00 0.00 0.21 0.00 0.00 0.09 0.05 0.00 9
0 1942 0.00 0.00 0.83 0.00 0.00 0.00 0.00 0.00 0.00 0.25 10
0 1942 0.54 0.00 1.24 0.05 0.01 0.46 0.32 0.10 0.07 0.00 11
0 1942 0.00 0.00 0.04 0.00 0.00 0.00 0.00 0.00 0.22 0.00 12
0 1942 0.00 0.00 0.01 0.00 0.00 0.00 0.04 0.00 0.00 0.02 13
0 1942 0.00 0.00 0.48 0.00 0.00 0.00 0.00 0.00 0.00 0.00 14
0 1942 0.00 0.00 0.00 0.00 0.63 0.01 0.46 0.36 0.84 0.10 15
0 1942 1.00 0.16 0.00 0.00 0.00 0.00 0.00 0.04 0.00 0.00 16
0 1942 1.04 0.24 0.00 0.00 0.00 0.00 0.00 0.13 0.00 0.00 17
0 1942 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.11 0.04 0.06 18
0 1942 0.00 1.19 0.00 0.00 0.00 0.00 0.00 0.00 0.53 0.29 19
0 1942 0.28 0.06 0.00 0.00 0.03 0.21 0.30 0.00 0.76 0.00 20
0 1942 0.00 0.00 0.00 0.00 0.30 2.11 0.00 0.00 0.01 0.00 21
0 1942 0.09 0.00 0.00 0.00 0.00 0.00 0.77 0.00 0.40 0.00 22
0 1942 0.00 0.00 0.66 0.10 0.00 0.01 0.00 0.00 0.00 0.00 23
0 1942 0.00 1.10 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 24
0 1942 0.00 0.00 0.00 0.00 0.40 0.00 0.00 0.00 0.00 0.00 25
0 1942 0.00 0.00 0.00 0.00 0.00 0.32 0.01 0.00 0.00 0.91 26
0 1942 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.98 27
0 1942 0.38 0.01 0.83 0.27 0.00 0.01 0.23 0.00 0.47 0.20 28
0 1942 0.00 0.00 0.30 0.00 0.00 0.04 0.11 0.00 0.00 0.00 29
0 1942 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 30
0 1942 0.00 0.00 0.49 1.84 1.87 0.00 0.00 0.00 0.28 0.11 31
0 1942 0.02 0.00 0.00 0.00 0.00 0.00 0.01 0.77 0.00 0.00 32
0 1942 0.00 0.00 0.21 0.66 0.03 0.18 0.32 0.00 0.00 0.00 33
0 1942 0.00 0.00 0.00 0.01 0.02 0.68 0.13 0.00 0.00 0.00 34
0 1942 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.10 35
0 1942 0.00 0.00 0.00 0.00 0.86 0.00 0.24 0.22 0.00 0.00 36
0 1942 0.00 0.00 0.00 0.00 0.00 0.00 37
For each year, it has 37 lines of numbers and each line contains 10
actual data points, then it starts over for the next year in exactly
the same format. I have a problem in my original bigger program in
fscanf to get these numbers. So I condensed it to this:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
main(int argc, char *argv[])
{
int i,yr;
long linenumber;
char filename[200];
char *sfname1,*sfname2,*sfname3;
FILE *fp;
FILE *otf;
FILE *fptest;
char dump[80];
char *line;
int dum,year,l;
float f1,f2,f3,f4,f5,f6,f7,f8,f9,f10, sum_mon;
i=0;yr=1941;linenumber=0;sum_mon = 0;
line = malloc(80); assert(line !=NULL); strcpy(line,"");
strcpy(filename,argv[1]);
fp = fopen(filename,"r"); if(!fp) printf("file open problems\n");
fptest = fopen("z.pcp.test","w+"); if(!fptest) printf("file open
problems\n");
for (yr=0; yr<2; yr++) { // year loop
for (i=1;i<=12; i++) {
//fgets(line, 80, fp);
fscanf(fp,"%d%d%f%f%f%f%f%f%f%f%f%f%d",&dum,&year,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&l);
}
//line 13
fscanf(fp,"%d%d%f%f%f%f%f%f%f%f%f%f%d",&dum,&year,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&l);
printf("%5d%5d%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%10d\n",dum,year,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,l);
for (i=14;i<=36; i++) {
//fgets(line, 80, fp);
fscanf(fp,"%d%d%f%f%f%f%f%f%f%f%f%f%d",&dum,&year,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&l);
}
fgets(line,80,fp);
}
fclose (fp);
}
The purpose is to just see why fscanf can't get the well formatted data
correctly in my original program. You can save the data file by c-n-p
the data
above in a file in the same directory as the above program and then at
prompt:
a.out filename to run the program. You will notice that first of all, I
want print out every line 13 in each year, but it print out line 14.
Secondly, it kept printing the last line in the first year! I am really
puzzled. Please help. I 've tried some other things already, I know I
can getline (char by char etc) or sscanf I am still not sure why the
above wouldn't work.
thanks.
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1
0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2
0 1941 0.00 0.00 0.00 0.00 0.52 0.00 0.00 0.17 1.07 0.09 3
0 1941 0.04 0.00 0.00 0.00 0.00 0.62 0.00 0.01 0.00 0.00 4
0 1941 0.00 0.02 0.00 0.00 0.00 0.22 0.00 0.00 0.00 0.16 5
0 1941 0.00 0.00 0.00 0.00 0.09 0.04 0.00 0.00 0.00 0.00 6
0 1941 0.00 0.83 0.00 0.00 0.00 0.00 0.00 0.00 0.22 0.03 7
0 1941 0.31 0.00 0.00 0.00 0.00 0.00 0.03 0.00 0.00 0.00 8
0 1941 0.00 0.48 0.38 0.07 0.00 0.12 0.00 0.00 0.00 0.00 9
0 1941 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.37 0.00 0.00 10
0 1941 1.13 0.01 0.00 0.00 0.00 0.00 0.00 0.07 0.00 0.00 11
0 1941 0.00 0.16 0.00 0.00 0.00 0.23 0.00 0.00 0.00 0.00 12
0 1941 0.01 0.10 0.00 0.00 0.00 0.00 0.00 0.00 0.13 0.00 13
0 1941 0.00 1.18 0.38 0.73 0.00 0.16 0.00 0.00 0.00 0.00 14
0 1941 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.30 0.00 0.00 15
0 1941 0.56 0.02 0.00 0.00 0.06 0.00 0.31 0.00 0.00 0.00 16
0 1941 0.00 0.00 0.00 0.34 0.29 0.32 0.00 0.00 0.00 0.00 17
0 1941 0.00 0.00 0.00 1.06 0.67 0.00 0.00 0.00 0.00 1.41 18
0 1941 0.00 0.00 0.00 0.00 0.52 0.00 0.00 0.00 0.00 0.29 19
0 1941 0.00 0.20 0.44 0.01 0.00 0.00 0.00 0.05 0.32 0.00 20
0 1941 0.00 0.00 0.00 0.00 0.75 0.00 0.00 1.10 0.00 1.87 21
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.00 22
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.28 0.78 0.00 0.00 23
0 1941 0.00 0.35 0.00 0.32 0.00 0.00 0.50 0.00 0.00 0.00 24
0 1941 0.00 0.00 0.00 0.00 1.38 0.25 1.16 0.00 0.00 0.00 25
0 1941 0.32 0.00 0.00 0.00 0.00 0.25 0.04 0.00 0.00 0.00 26
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 27
0 1941 0.16 0.22 0.00 0.00 0.16 0.00 0.00 0.00 0.00 0.00 28
0 1941 0.00 0.00 1.24 0.00 0.10 0.07 0.02 0.02 0.02 0.00 29
0 1941 0.25 0.04 0.56 0.27 0.00 0.00 0.00 0.00 0.00 0.00 30
0 1941 0.00 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.01 31
0 1941 0.51 0.01 0.46 0.00 0.00 0.92 0.00 0.00 0.00 0.00 32
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.24 0.00 0.00 33
0 1941 0.00 0.00 0.43 0.19 0.00 0.00 0.00 0.00 0.00 0.00 34
0 1941 0.00 0.00 0.05 0.12 0.38 0.26 0.01 0.00 0.00 0.00 35
0 1941 0.00 0.01 0.71 0.00 0.00 0.20 0.13 0.62 0.90 0.00 36
0 1941 0.00 0.01 0.02 0.04 0.00 0.00 37
0 1942 0.00 0.00 0.00 0.00 0.02 0.03 0.00 0.00 0.00 0.00 1
0 1942 0.00 0.02 0.00 0.00 0.08 0.00 0.00 0.00 0.00 0.00 2
0 1942 0.00 0.00 0.00 0.00 0.10 0.00 0.00 0.83 0.90 0.18 3
0 1942 0.00 0.00 0.00 0.00 0.31 0.00 0.00 0.00 0.00 0.00 4
0 1942 0.31 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 5
0 1942 0.00 0.00 0.00 0.58 0.00 0.00 0.00 0.00 0.12 0.00 6
0 1942 0.00 0.00 0.00 0.00 0.04 0.31 0.00 0.00 0.00 0.00 7
0 1942 0.00 0.13 0.03 0.17 0.37 0.00 0.00 0.00 0.00 0.00 8
0 1942 0.00 1.03 0.00 0.00 0.21 0.00 0.00 0.09 0.05 0.00 9
0 1942 0.00 0.00 0.83 0.00 0.00 0.00 0.00 0.00 0.00 0.25 10
0 1942 0.54 0.00 1.24 0.05 0.01 0.46 0.32 0.10 0.07 0.00 11
0 1942 0.00 0.00 0.04 0.00 0.00 0.00 0.00 0.00 0.22 0.00 12
0 1942 0.00 0.00 0.01 0.00 0.00 0.00 0.04 0.00 0.00 0.02 13
0 1942 0.00 0.00 0.48 0.00 0.00 0.00 0.00 0.00 0.00 0.00 14
0 1942 0.00 0.00 0.00 0.00 0.63 0.01 0.46 0.36 0.84 0.10 15
0 1942 1.00 0.16 0.00 0.00 0.00 0.00 0.00 0.04 0.00 0.00 16
0 1942 1.04 0.24 0.00 0.00 0.00 0.00 0.00 0.13 0.00 0.00 17
0 1942 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.11 0.04 0.06 18
0 1942 0.00 1.19 0.00 0.00 0.00 0.00 0.00 0.00 0.53 0.29 19
0 1942 0.28 0.06 0.00 0.00 0.03 0.21 0.30 0.00 0.76 0.00 20
0 1942 0.00 0.00 0.00 0.00 0.30 2.11 0.00 0.00 0.01 0.00 21
0 1942 0.09 0.00 0.00 0.00 0.00 0.00 0.77 0.00 0.40 0.00 22
0 1942 0.00 0.00 0.66 0.10 0.00 0.01 0.00 0.00 0.00 0.00 23
0 1942 0.00 1.10 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 24
0 1942 0.00 0.00 0.00 0.00 0.40 0.00 0.00 0.00 0.00 0.00 25
0 1942 0.00 0.00 0.00 0.00 0.00 0.32 0.01 0.00 0.00 0.91 26
0 1942 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.98 27
0 1942 0.38 0.01 0.83 0.27 0.00 0.01 0.23 0.00 0.47 0.20 28
0 1942 0.00 0.00 0.30 0.00 0.00 0.04 0.11 0.00 0.00 0.00 29
0 1942 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 30
0 1942 0.00 0.00 0.49 1.84 1.87 0.00 0.00 0.00 0.28 0.11 31
0 1942 0.02 0.00 0.00 0.00 0.00 0.00 0.01 0.77 0.00 0.00 32
0 1942 0.00 0.00 0.21 0.66 0.03 0.18 0.32 0.00 0.00 0.00 33
0 1942 0.00 0.00 0.00 0.01 0.02 0.68 0.13 0.00 0.00 0.00 34
0 1942 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.10 35
0 1942 0.00 0.00 0.00 0.00 0.86 0.00 0.24 0.22 0.00 0.00 36
0 1942 0.00 0.00 0.00 0.00 0.00 0.00 37
For each year, it has 37 lines of numbers and each line contains 10
actual data points, then it starts over for the next year in exactly
the same format. I have a problem in my original bigger program in
fscanf to get these numbers. So I condensed it to this:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
main(int argc, char *argv[])
{
int i,yr;
long linenumber;
char filename[200];
char *sfname1,*sfname2,*sfname3;
FILE *fp;
FILE *otf;
FILE *fptest;
char dump[80];
char *line;
int dum,year,l;
float f1,f2,f3,f4,f5,f6,f7,f8,f9,f10, sum_mon;
i=0;yr=1941;linenumber=0;sum_mon = 0;
line = malloc(80); assert(line !=NULL); strcpy(line,"");
strcpy(filename,argv[1]);
fp = fopen(filename,"r"); if(!fp) printf("file open problems\n");
fptest = fopen("z.pcp.test","w+"); if(!fptest) printf("file open
problems\n");
for (yr=0; yr<2; yr++) { // year loop
for (i=1;i<=12; i++) {
//fgets(line, 80, fp);
fscanf(fp,"%d%d%f%f%f%f%f%f%f%f%f%f%d",&dum,&year,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&l);
}
//line 13
fscanf(fp,"%d%d%f%f%f%f%f%f%f%f%f%f%d",&dum,&year,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&l);
printf("%5d%5d%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%5.2f%10d\n",dum,year,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,l);
for (i=14;i<=36; i++) {
//fgets(line, 80, fp);
fscanf(fp,"%d%d%f%f%f%f%f%f%f%f%f%f%d",&dum,&year,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&l);
}
fgets(line,80,fp);
}
fclose (fp);
}
The purpose is to just see why fscanf can't get the well formatted data
correctly in my original program. You can save the data file by c-n-p
the data
above in a file in the same directory as the above program and then at
prompt:
a.out filename to run the program. You will notice that first of all, I
want print out every line 13 in each year, but it print out line 14.
Secondly, it kept printing the last line in the first year! I am really
puzzled. Please help. I 've tried some other things already, I know I
can getline (char by char etc) or sscanf I am still not sure why the
above wouldn't work.
thanks.