Reputation: 383
I am starting to program in C, and I am doing a program in which it adds users to a file.
I have managed to add users to the file and I can see what users I have added while the program is still running.
But when I close the program and open again, if I press to see the users it doesn't show anything because it doesn't load it from the file.
How can I create a function that will read the whole file when I run the program and display the users in the file when I select the option show users at the beginning??
the file contains users with three fields Id name and surname.
Upvotes: 0
Views: 883
Reputation: 643
When you are writing to the file make sure you open it in append mode otherwise the program will erase the previous contents of the file.
To open in append mode use this:
FILE *F=fopen("filename","a");
Upvotes: 0
Reputation: 20244
Here is what you should do:
fgets
.strtok
to break the line and then,parse the ID,name and surname.Put the above 3 in a loop which exits when fgets
returns 0 or EOF
.
Upvotes: 1