无法让sscanf在某些输入下正常工作

时间:2018-05-18 18:58:34

标签: c scanf

我正试图用sscanf读取这些行中的值到变量,我得到了非常奇怪的结果。只要我使用浮点数,它就可以使用某些行,但是使用其他类似的浮点数它不起作用,如果我使用双精度而不是浮点数它就永远无法正常工作。

#include <stdio.h>
#include <stdlib.h>

void debug(char* line,int* year, int* month, int* day, double* temp, double* uncertainty,
char country[100]){
    int result;
    result = sscanf(line, "%i - %i - %i, %lf , %lf , %s", year, 
    month, day, temp, uncertainty, country);
    printf("%i-%i-%i,%f,%f,%s\n",*year, *month, *day, *temp, 
    *uncertainty, country);
    printf("Result:%i\n", result);
}

void debug_f(char* line, int* year, int* month, int* day, float* temp, float* uncertainty,
char country[100]){
    int result;
    result = sscanf(line, "%i - %i - %i, %f , %f , %s", year, 
    month, day, temp, uncertainty, country);
    printf("%i-%i-%i,%lf,%lf,%s\n",*year, *month, *day, *temp, 
    *uncertainty, country);
    printf("Result:%i\n", result);
}

int main(){
    char* error = "1943-09-01,29.27,0.403,Yemen";
    char* working = "1972-03-01,4.787,0.342,Slovakia";
    int year1, year2, year3, year4;
    int month1, month2, month3, month4;
    int day1, day2, day3, day4;
    double temp1, temp2;
    double uncertainty1, uncertainty2;
    float temp3, temp4;
    float uncertainty3, uncertainty4;
    char country1[100], country2[100], country3[100], country4[100];
    debug(error, &year1, &month1, &day1, &temp1, &uncertainty1, country1);
    debug(working, &year2, &month2, &day2, &temp2, &uncertainty2, country2);
    debug_f(error, &year3, &month3, &day3, &temp3, &uncertainty3, country3);
    debug_f(working, &year4, &month4, &day4, &temp4, &uncertainty4, country4);
}

这是我在我的机器上得到的输出:

1943-0-0,0.000000,0.000000,\

结果:2

1972-3-1,0.000000,0.000000,斯洛伐克

结果:6

1943-0-0,0.000000,0.000000,

结果:2

1972-3-1,4.787000,0.342000,斯洛伐克

结果:6

2 个答案:

答案 0 :(得分:1)

尝试删除sscanf中日期元素之间使用的空格。

sscanf(line, "%i-%i-%i, %f , %f , %s", 
               &int1, &int2,
               &int3, &double1, &double2,
               s);

正如EugeneSh指出其有符号整数,并将float读取为double。

答案 1 :(得分:0)

“%i”与“%d”在扫描时不同

"1943-09-01,29.27,0.403,Yemen";失败,因为"09"未被int扫描为"%i"的9。

在下面的代码中,当month扫描sscanf()时,使用"%i"遇到"09"。由于它以'0'开头,因此将解释转换为八进制。由于'9'不是八进制数字,因此month被分配为0,并且扫描以" - %i, %lf , %lf , %s"继续。由于'9''-'不匹配,因此扫描将停止并报告2次成功的转换。

sscanf(line, "%i - %i - %i, %lf , %lf , %s", year,  month, day, temp, uncertainty, country);

建议的替代方法

使用"%d"而非"%i"来确保十进制解释。

保存字符串时使用宽度限制。

使用"%n"检测扫描结束并查找多余的垃圾。

void debug(const char* line, int* year, int* month, int* day, 
    double* temp, double* uncertainty, char country[100]){
  int n = 0;
  int result = sscanf(line, "%d - %d - %d , %lf , %lf , %99s %n", 
      year, month, day, temp, uncertainty, country, &n);
  printf("Result:%i\n", result);

  if (n > 0 && line[n] == '\0') {
    printf("%i-%i-%i,%f,%f,%s\n",
        *year, *month, *day, *temp, *uncertainty, country);
  } else {
    printf("<%s> failed\n", line);
  }
}

请注意,%s不会完全读取"Sri Lanka"之类的国家名称,而只会读取第一个“单词” "Sri"。根据编码目标,请考虑:

  int result = sscanf(line, "%d - %d - %d , %lf , %lf , %99[^\n] %n",