e.f.a.
e.f.a.

Reputation: 383

loadFile() function in C to load users from a file at the time I execute the program

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

Answers (2)

rahul tyagi
rahul tyagi

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

Spikatrix
Spikatrix

Reputation: 20244

Here is what you should do:

  1. Read each line one by one by using fgets.
  2. Use strtok to break the line and then,parse the ID,name and surname.
  3. Print them.

Put the above 3 in a loop which exits when fgets returns 0 or EOF.

Upvotes: 1

Related Questions