Hi all. I am trying to find a way to read a file which has 10 lines and 4 columns of numbers and assign it to my arrays. The file itself is opening ok but for some reason it only reads the last line.
Code:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main( void )
{
int number[10] = {0};
int value1[10] = {0};
int value2[10] = {0};
int value3[10] = {0};
FILE* readThis;
if ((readThis = fopen ("file.txt", "r"
)) ==NULL) {
printf("\nError opening file \n");
exit(101);
}
while (!feof(readThis)) {
fscanf( readThis, "%d %d %d %d", &number, &value1, &value2, &value3 );
}
fclose(readThis);
printf("\n\n%d\n\n%d\n\n%d\n\n%d\n\n", number[0], value1[0], value2[0], value3[0]);//This prints the last line of the file
printf("\n\n%d\n\n%d\n\n%d\n\n%d\n\n", number[1], value1[1], value2[1], value3[1]);//This prints all zeros
return 0;
}
any help would be appreciated thanks.