Search code examples
cfopen

Append to the end of file with Write permissions


I was trying to code and ran into a problem.

FILE *tickets = fopen("tickets.h", "w"); - so here I open a file source where in the future I want to append some text to the end of the file. So the problem is that I don't want the file content to be removed and want to append text to the end of the file. I've chosen write permissions because in the future the program will be writing doing stuff with the write and not the append perms.

Help will be appreciated!


Solution

  • You should use FILE *tickets = fopen("tickets.h", "a");

    Because when you open a file in the write mode, the file is overwritten, resulting in deletion of any data already present in the file. But in append mode this will not happen. Append mode is used to append or add data to the existing data of file(if any).