Spyros Mouchlianitis
Spyros Mouchlianitis

Reputation: 125

How to open a file in C without losing its previous contents?

If the file doesn't exist I want to create it. But the next time that the program runs I want to open the file without losing its previous contents. Can anyone help? I have one function that loads the file and another that saves the file. I just don't know what mode I should use

Upvotes: 0

Views: 1049

Answers (1)

Shubham Gharage
Shubham Gharage

Reputation: 11

You can update the already existing text with a call to fopen using append mode, so to update this previously created file without erasing the data you can use the mode "a" like this:

ptr = fopen("file_name.txt","a") //(a=append)

This will add your fprintfed strings or other inputted data at the end of the .txt file, instead of overwriting from the beginning

Upvotes: 1

Related Questions