0

I have a file with Space Separated Values Eg:

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

And I can't retrieve them to variables using the following code:

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

And

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

doesn't work I want this to update the variables content until it reaches the end of file.

Andrej Hatzi
  • 312
  • 4
  • 18
  • 6
    Did you mean to do `!= EOF`? But even that is not correct. Better to have `== 3`. – kaylum Mar 15 '20 at 03:42
  • 2
    Don't put `\n` in the format string with `scanf()`. Numbers are automatically delimited by whitespace, you don't need that. It will cause a problem if the last line doesn't end with newline. – Barmar Mar 15 '20 at 03:54
  • I've tried first without \n and it didn't work, and with ==3 it didn't work also... – Andrej Hatzi Mar 15 '20 at 03:59
  • 1
    Then please describe the problem in more detail than "doesn't work". What exactly are you observing? – kaylum Mar 15 '20 at 04:03
  • in the `if (f == NULL)` case you should then abort the program, not go on to call fscanf – M.M Mar 15 '20 at 04:04
  • in `main`, `V[0]` causes undefined behaviour since `V` points nowhere – M.M Mar 15 '20 at 04:04
  • It doesn't show up anything when printing the variables – Andrej Hatzi Mar 15 '20 at 04:25
  • @AndrejHatzi you will need to debug your program; if you make all of the changes that have been suggested and your file is as shown then the program would work – M.M Mar 15 '20 at 04:27

1 Answers1

2

Wrong check. Should be:

int status;
while((status = fscanf(f, "%d%d%d\n", &nAluno, &nTeste, &nTrab)) > 0) {
    if (status == 3) {
        printf("%d %d %d\n", nAluno, nTeste, nTrab);
    }
}
Nicolae Natea
  • 1,185
  • 9
  • 14
  • no, you should check the number of values returned. For example it may return `0` if the file contained a string. There's basically never any reason to check against EOF with scanf family – M.M Mar 15 '20 at 04:02
  • If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned. – Nicolae Natea Mar 15 '20 at 04:07
  • Basically he should check the value is greater than 0. – Nicolae Natea Mar 15 '20 at 04:10
  • testing `==3` seems best, as if it were 1 or 2 then that is an error that needs to be handled (some of the output fields will be incorrect or garbage) – M.M Mar 15 '20 at 04:11
  • I agree with your comments, but, the main problem was that in his code he never enters the loop. And the answer indicates the flaw. It doesn't say anywhere that all the variables should be updated or that the data should not be corrupt. That might be the intention of reporter but it's not explicit from the request. – Nicolae Natea Mar 15 '20 at 04:23