c - 对文件内容使用 atoi() 时出现意外结果,逐字符读取
问题描述
我有一个文本文件,其中只有数字(0、1、2 和 3),我想处理数据以了解每个数字出现的次数。
以下程序适用于小文本文件(<100 个数字),但对于较大的文件(我最终需要处理数千个数据),程序会读取文本文件中没有的数字。
这是我的代码:
FILE *file;
char c;
int nb;
int th0 = 0, th1 = 0, th2 = 0, th3 = 0;
file = fopen("../data", "r");
if (file == NULL) {
printf("ERROR FILE: %s", strerror(errno));
return EXIT_FAILURE;
}
while (1) {
if (fscanf(file, "%c", &c) == EOF)
break;
nb = atoi(&c);
printf("%d", nb);
switch (nb) {
case 0:
th0++;
break;
case 1:
th1++;
break;
case 2:
th2++;
break;
case 3:
th3++;
break;
default:
break;
}
}
我将不胜感激任何建议。
编辑,带有输出的输入文本:
数据文件(181 个数字):
001110120101010102012012021201021202102012012012012010210210210120120230103120130230123201320310231023102302301231203213210131032103210230120320310320213202301320123120312302321023
解决方案
你的问题是atoi
需要一个字符串,你这样称呼它:
nb = atoi(&c);
c
只是一个char
. 有时这可能会起作用,但您基本上会遇到未定义的行为,因为您不能保证之后的内存c
是空的。
相反,您想要以nb
不同的方式计算。
nb = c - '0';
这依赖于这样一个事实,即在 ASCII 表中,数字 0 到 9 一起在一个块中。减去“0”的值c
将得到该字符的数值......假设它是一个数字。
为了确保它是一个数字,您应该将此if
语句包装在您的代码周围
if(isdigit(c)) // Check if c is a digit
{
nb = c - '0';
switch(nb) {
case 0: th0++;
break;
// rest of switch goes here....
}
}
推荐阅读
- r - 根据值列表有效地检查多个变量的值
- python - 有没有办法使用用户输入来调用函数?
- swift - 未使用正确的值观察到已发布的 var
- excel - 如何使用 VBA 自动调整列宽?
- r - ggplot:geom_text 不在 geom_col 上方打印
- owl - 如何比较OWL中的属性?
- excel - Excel使用求和函数并删除求和数
- mysql - 使用 sidecar 方法将我的 kubernetes 引擎应用程序连接到谷歌云 sql 实例时遇到问题
- python - Pyautogui 函数 locateonscreen 给出属性错误
- java - LWJGL glGenVertexArrays() 正在阻止执行