c - 从 C 中的文件中读取一行并提取输入的数量
问题描述
我有一个文件input.dat。在这个文件中,有 3 行:
1 2 3
5 7 10 12
8 9 14 13 15 17
我将使用 C 读取三行之一,并返回元素的数量。例如,我想将第 2 行读5 7 10 12
入内存,并返回第 2 行中的值的个数,即4
. 我的代码如下...
#include <stdio.h>
#include <stdlib.h>
#define STRING_SIZE 2000
int main() {
FILE *fp = fopen("in.dat", "r");
char line[STRING_SIZE];
int lcount = 0, nline = 1, sum = 0, number;
if (fp != NULL) {
while (fgets(line, STRING_SIZE, fp) != NULL) {
if (lcount == nline) {
while (sscanf(line, "%d ", &number)) {
sum++;
}
break;
} else {
lcount++;
}
}
fclose(fp);
}
exit(0);
}
当我运行这段代码时,它永远不会像死循环一样停止。这里有什么问题?
解决方案
chqrlie答案的更简洁的版本。以字符串开头,因为这就是问题的真正意义 after fgets()
。
sscanf()
不会单步执行字符串,它总是从头开始读取。
strtol()
在字符串的开头查找 a long int
,忽略初始空格。返回停止扫描的地址。
的手册strtol()
说应该检查 errno 是否有任何转换错误。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define STRING_SIZE 2000
int main(void)
{
char line[STRING_SIZE] = "5 7 10 12";
char* start = line;
char* end;
int count = 0;
while(1)
{
/**
* strtol() look for long int in beginning of the string
* Ignores beginning whitespace
*
* start: where to strtol() start looking for long int
* end: where strtol() stops scanning for long int
*/
errno = 0; // As strol() manual says
strtol(start, &end, 0);
if (errno != 0)
{
printf("Error in strtol() conversion.\n");
exit(0);
}
if (start == end) break; // Quit loop
start = end;
count++;
}
printf("count: %d\n", count);
return 0;
}
推荐阅读
- flutter - 为 Sliver 孩子设置自定义高度
- javascript - 为什么 ReactJS 组件刷新页面后消失
- dart - 在 VSCode 中重构 Dart 不起作用
- concurrency - 为什么'if let'会使用互斥锁阻止执行?
- python - cx_Freeze 包在另一台机器上不起作用
- django - 如何对篮子线申请折扣?
- oracle - 使用多个类似条件使用另一个表中的值更新一个巨大的表
- jenkins - 是的,我可以在 jenkins 中启动一个守护进程。但是如何重新启动守护进程呢?@视窗
- sql-server - 如何在一个查询中清除所有系统版本表?
- python - 如何根据熊猫中的多列条件删除行?