首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使sscanf在某些输入下正常工作时出现问题

使sscanf在某些输入下正常工作时出现问题
EN

Stack Overflow用户
提问于 2018-05-19 02:58:34
回答 2查看 48关注 0票数 1

我试图用sscanf将这些行中的值读入变量,但得到了非常奇怪的结果。只要我使用浮点数,它就可以处理一些行,但对于其他类似的浮点数行,它就不能工作,如果我使用双精度浮点数而不是浮点数,它永远不会正常工作。

代码语言:javascript
复制
#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.00000,0.000000,�\��

结果:2

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

结果:6

1943-0-0,0.00000,0.00000,

结果:2

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

结果:6

EN

回答 2

Stack Overflow用户

发布于 2018-05-19 03:04:57

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

代码语言:javascript
复制
sscanf(line, "%i-%i-%i, %f , %f , %s", 
               &int1, &int2,
               &int3, &double1, &double2,
               s);

因为EugeneSh指向其有符号整数上方,并将浮点数读取为双精度。

票数 1
EN

Stack Overflow用户

发布于 2018-11-09 02:57:30

扫描时,"%i“与"%d”不同

"1943-09-01,29.27,0.403,Yemen";失败,因为"09"未扫描到带有"%i"int as 9中。

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

代码语言:javascript
复制
sscanf(line, "%i - %i - %i, %lf , %lf , %s", year,  month, day, temp, uncertainty, country);

建议的替代方案

使用"%d"而不是"%i"来确保小数解释。

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

使用"%n"检测扫描结束并查找额外的垃圾文件。

代码语言:javascript
复制
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"。根据编码目标,请考虑:

代码语言:javascript
复制
  int result = sscanf(line, "%d - %d - %d , %lf , %lf , %99[^\n] %n", 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50417719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档