如何在 c 中使用 sscanf 从字符串中读取所有双精度数?
How to read all the double numbers from a string using sscanf in c?
我有一个文本文件,其中包含不同格式的行。我想用 c 读取这个文本文件中的所有数字。我想将这些数字读取为双精度数字。文本文件内容如下:
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
我用fopen读取文件,用sscanf单行扫描内容。我目前的代码如下:
#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);
代码的输出是
: 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
它不包含我的文本文件中的所有数字。
我修改了下面的代码以输出行中的所有数字。
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);
我有一个文本文件,其中包含不同格式的行。我想用 c 读取这个文本文件中的所有数字。我想将这些数字读取为双精度数字。文本文件内容如下:
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
我用fopen读取文件,用sscanf单行扫描内容。我目前的代码如下:
#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);
代码的输出是
: 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
它不包含我的文本文件中的所有数字。
我修改了下面的代码以输出行中的所有数字。
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);