I have a text file which contains lines in different formats. I want to read all the numbers in this text file using c. And I want to read the numbers as a double precision number. The content of the text file is below:
1,' ', 13.8000,2, 0.000, 0.000, 1, 1,1.04500, 11.3183, 1
2,' ', 13.8000,2, 0.000, 0.000, 1, 1,0.98000, 19.9495, 1
17,'1 ',1, 1, 1, 6000.000, 300.000, 0.000, 0.000, 0.000, 0.000, 1
16,'16', 4000.000, 401.887, 9999.000, -9999.000,1.00000, 0, 200.000, 0.00000, 0.00550
I used fopen to read the file and used sscanf to scan the content in a single line. The code I have so far is below:
#include <stdio.h>
#include <string.h>
FILE *fptr;
if ((fptr = fopen("rggs.raw","r")) == NULL){
fprintf(mapFile,"Error! opening file");
}
int line=0;
char input[512];
int total_n = 0;
double ii;
int nn;
while( fgets( input, 512,fptr)){
line++;
total_n = 0;
while (1 == sscanf(input + total_n, "%lf", &ii, &nn)){
total_n += nn;
printf(": %lf\n", ii);
}
}
printf("\n\nEnd of Program\n");
fclose(fptr);
The output of the code is
: 1.000000
: 2.000000
: 17.000000
: 1.000000
: 0.000000
: 0.000000
: 0.000000
: 16.000000
: 0.000000
: 9.000000
: 0.000000
: 0.000000
: 550.000000
End of Program
and it doesn't contain all the numbers in my text file.
I modified the code as below to output all the numbers in the line.
FILE *fptr;
if ((fptr = fopen("raw_data_IEEE_68.raw","r")) == NULL){
fprintf(mapFile,"Error! opening file");
}
int line=0;
char input[512];
int total_n = 0;
double ii;
int nn;
while( fgets( input, 512,fptr)){
line++;
total_n = 0;
while (1 == sscanf(input + total_n, "%lf%*[,' ]%n", &ii, &nn)){
total_n += nn;
printf(": %lf\n", ii);
}
}
printf("\n\nEnd of Program\n");
fclose(fptr);