fscanf不会检索任何值

问题描述 投票:0回答:2

我有一个带有空格分隔值的文件例如:

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);
    }

不起作用,我希望它能更新变量内容,直到到达文件末尾。

c loops scanf c99
2个回答
1
投票

检查不正确。应该是:


-1
投票
#include <stdio.h>
#include <stdlib.h>
void ReadVector(int V[], int *N);
int CalcularAprovados(int V[], int N);
void ReadVector(int V[], int *N){ // Where are these parameters used?
    FILE *f;
    f = fopen("dados4.txt", "r");
    if (f == NULL){
        printf("Error");
       // Should return here as the is no reason to continue
       return;
    }
    int nAluno, nTeste, nTrab;
    while(fscanf(f, "%d%d%d\n", &nAluno, &nTeste, &nTrab) == 3){ // Please see the manual page
        //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); // V has no value!
}
© www.soinside.com 2019 - 2024. All rights reserved.