如何在c中使用sscanf从字符串中读取所有双精度数字?

问题描述 投票:1回答:1

我有一个文本文件,其中包含不同格式的行。我想使用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

并且它不包含我的文本文件中的所有数字。

c file scanf fopen
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.