【发布时间】:2020-03-15 03:38:59
【问题描述】:
我有一个包含空格分隔值的文件 例如:
6028 5 6
9813 2 10
10249 7 8
10478 8 8
10479 3 2
10516 6 3
10519 9 10
10525 3 7
10606 6 1
10611 6 9
10632 1 6
10638 9 4
而且我无法使用以下代码将它们检索到变量中:
#include <stdio.h>
#include <stdlib.h>
void ReadVector(int V[], int *N);
int CalcularAprovados(int V[], int N);
void ReadVector(int V[], int *N){
FILE *f;
f = fopen("dados4.txt", "r");
if (f == NULL){
printf("Error");
}
int nAluno, nTeste, nTrab;
while(fscanf(f, "%d%d%d\n", &nAluno, &nTeste, &nTrab) == EOF){
//fscanf(f, "%d %d %d", &nAluno, &nTeste, &nTrab);
printf("%d %d %d\n", nAluno, nTeste, nTrab);
}
fclose(f);
}
int main(){
int *V, N=0;
ReadVector(&V[0], &N);
}
还有
int nAluno, nTeste, nTrab;
while(fscanf(f, "%d%d%d\n", &nAluno, &nTeste, &nTrab) == EOF){
//fscanf(f, "%d %d %d", &nAluno, &nTeste, &nTrab);
printf("%d %d %d\n", nAluno, nTeste, nTrab);
}
不起作用我希望它更新变量内容,直到它到达文件末尾。
【问题讨论】:
-
你的意思是
!= EOF?但即使这样也不正确。最好有== 3。 -
不要将
\n放在带有scanf()的格式字符串中。数字会自动由空格分隔,您不需要。如果最后一行不以换行符结尾会导致问题。 -
我先试过不带 \n 还是不行,加上 ==3 也不行...
-
那么请比“不起作用”更详细地描述问题。你到底在观察什么?
-
在
if (f == NULL)的情况下,您应该中止程序,而不是继续调用 fscanf