Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear the whole contents of a file in C?

I have a file with some of user1's data. I want to use the same file for user2 by clearing the content of the file.

My idea is that when a new user comes, data of the previous user should be clear and the same file should be ready for the new user.

like image 408
mujahid Avatar asked Sep 05 '25 16:09

mujahid


2 Answers

As @stefan said using fopen() with "w" mode will do the job for you. When you open a file with "w" flag it creates an empty file for writing. If a file with the same name already exists its contents are erased and the file is treated as an empty new file.

If the file is already open you can use freopen() function from stdio.h with "w" mode as it will first close the file and then reopen it for writing erasing whatever was in the file previously.

like image 200
binW Avatar answered Sep 07 '25 18:09

binW


with fopen(filename, flag) just open it with flag= "w" or "wb" and it will be cleared

like image 33
stefan Avatar answered Sep 07 '25 18:09

stefan