Reputation: 490
I am trying to read multiple integers from a 500Mb text file where the integers are int the format
1000002 1 55
1000002 1000006 33
1000002 1000007 8
1000002 1000009 144
When i try and read the integers i have the problem where the program does not read enough the entire file and of that file that it read's it does not seem to be the correct values my code is
FILE *file;
file = fopen("user_artist_data.txt", "r");
int User_ID, artist_ID, Play_Count = 0;
int check = 0;
while(fscanf(file, "%d %d %d", &User_ID, &artist_ID, &Play_Count) != 0)
{
check++;
printf("%d %d %d\n", User_ID, artist_ID, Play_Count);
}
fclose(file);
Can you please help me with fixing my code. Thank you
Upvotes: 1
Views: 3146
Reputation: 1
Read carefully the documentation of fscanf(3) (you probably should read it more than once).
Then you'll understand that you probably should code
while(fscanf(file, " %d %d %d", &User_ID, &artist_ID, &Play_Count) == 3)
(notice that spaces are handled like newlines by fscanf
)
BTW, if your file is line oriented, you'll better read each line with e.g. getline(3) (or else fgets
if getline
is not available) then parse each line with sscanf(3). The %n
conversion specification is probably very useful.
Don't forget to compile with all warnings and debug info (e.g. gcc -Wall -Wextra -g
if using GCC...) and to use your debugger (e.g. gdb
)
Upvotes: 1