当循环遍历文本文件时意外停止
While loop through text file stops unexpectedly
我正在尝试遍历包含随机内容的文本文件。它的当前内容是:
"13 -35 57 - 23723724
12taste-34the+56rain-bow845"
我的程序应该只从文件中获取数字(-35
作为负数,而不是 - 23723724
,因为中间有 space)并且没有字母或其他字符与整数无关。
目前我的代码有一个 while 循环遍历文件并获取所有十进制值。然而,由于某些未知原因,它在 57 后停止(总结果为:“13-3557”,然后停止)。
我试图分别遍历每个字符,但这带来了它自己的一系列问题,并且这种方法至少 returns 个整数。
这是我的代码:
int *getIntegers(char *filename, int *pn) {
// Create a dynamic array
int len = 100;
int *numbers = malloc(sizeof(int) * len);
// Source file
FILE *file;
file = fopen(filename, "r");
int i = 0, number = 0;
while(fscanf(file, "%d", &number) > 0) {
numbers[i++] = number;
printf("%d", number);
}
return numbers;
}
编辑:
我更改了我的代码,它现在检索所有数字,但没有 spaces.
// Create a dynamic array
int len = 100;
int *numbers = malloc(sizeof(int) * len);
// Source file
FILE *file;
file = fopen(filename, "r");
int i = 0, number = 0;
while(!feof(file)) {
if(fscanf(file, "%d ", &number) > 0) {
numbers[i++] = number;
} else {
clearerr(file);
fgetc(file);
}
}
fclose(file);
return numbers;
fscanf
不会一直查看其输入,直到找到与其模式匹配的内容。在这种情况下,它遇到了孤独的-
,无法将其解析为整数,returns 零。这打破了你的循环。您将需要使用 EOF 来打破循环。
这是因为 fscanf
看到了孤独的 '-'
并且因为那不是一个有效的数字所以它无法解析它并且 returns 0
导致你的循环结束。
我建议您使用 fgets
to read the whole line, and then use strtok
to separate on space, and strtol
将标记化的字符串转换为数字。
当输入流遇到 -
并且它希望看到一个整数时,它不会读取任何内容。它到此为止。
如果您想继续阅读其余的数字,您需要一些代码来读取下一个字符,丢弃它,然后继续。
while(!foeof(file) )
{
if ( fscanf(file, "%d", &number) > 0) {
numbers[i++] = number;
printf("%d", number);
else {
clearerr(file); // Clear the error state.
fgetc(file); // Read the next character and discard it.
}
}
更新
要在输出中的数字之间添加 space,请使用:
printf("%d ", number);
我正在尝试遍历包含随机内容的文本文件。它的当前内容是:
"13 -35 57 - 23723724
12taste-34the+56rain-bow845"
我的程序应该只从文件中获取数字(-35
作为负数,而不是 - 23723724
,因为中间有 space)并且没有字母或其他字符与整数无关。
目前我的代码有一个 while 循环遍历文件并获取所有十进制值。然而,由于某些未知原因,它在 57 后停止(总结果为:“13-3557”,然后停止)。
我试图分别遍历每个字符,但这带来了它自己的一系列问题,并且这种方法至少 returns 个整数。
这是我的代码:
int *getIntegers(char *filename, int *pn) {
// Create a dynamic array
int len = 100;
int *numbers = malloc(sizeof(int) * len);
// Source file
FILE *file;
file = fopen(filename, "r");
int i = 0, number = 0;
while(fscanf(file, "%d", &number) > 0) {
numbers[i++] = number;
printf("%d", number);
}
return numbers;
}
编辑: 我更改了我的代码,它现在检索所有数字,但没有 spaces.
// Create a dynamic array
int len = 100;
int *numbers = malloc(sizeof(int) * len);
// Source file
FILE *file;
file = fopen(filename, "r");
int i = 0, number = 0;
while(!feof(file)) {
if(fscanf(file, "%d ", &number) > 0) {
numbers[i++] = number;
} else {
clearerr(file);
fgetc(file);
}
}
fclose(file);
return numbers;
fscanf
不会一直查看其输入,直到找到与其模式匹配的内容。在这种情况下,它遇到了孤独的-
,无法将其解析为整数,returns 零。这打破了你的循环。您将需要使用 EOF 来打破循环。
这是因为 fscanf
看到了孤独的 '-'
并且因为那不是一个有效的数字所以它无法解析它并且 returns 0
导致你的循环结束。
我建议您使用 fgets
to read the whole line, and then use strtok
to separate on space, and strtol
将标记化的字符串转换为数字。
当输入流遇到 -
并且它希望看到一个整数时,它不会读取任何内容。它到此为止。
如果您想继续阅读其余的数字,您需要一些代码来读取下一个字符,丢弃它,然后继续。
while(!foeof(file) )
{
if ( fscanf(file, "%d", &number) > 0) {
numbers[i++] = number;
printf("%d", number);
else {
clearerr(file); // Clear the error state.
fgetc(file); // Read the next character and discard it.
}
}
更新
要在输出中的数字之间添加 space,请使用:
printf("%d ", number);