30

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.

Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
mujahid
  • 691
  • 2
  • 12
  • 21

6 Answers6

53

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.

binW
  • 13,220
  • 11
  • 56
  • 69
  • 1
    For a file that had 0666 permissions, will the file still hold its 666 permissions or will umask come into play and set it to 644. Essentially I'm trying to know if it clears the contents of the file or will it delete the file and recreate one with the same name (in which case it would be made with 644) – Sam Thomas May 29 '18 at 21:12
18
fclose(fopen("file.txt", "w"));

Why this works:

write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.

(quote from http://www.cplusplus.com/reference/cstdio/fopen/)

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
18

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

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
stefan
  • 2,886
  • 21
  • 27
  • 1
    thanx stefan , but i want to clear the FILE FORCEFULLY (explicitly) – mujahid Jan 27 '11 at 10:42
  • ok gys!! but during prog. exec. em opening/closing (appending) many times. IWANT TO CLEAR THE FILE ONLY WHEN SOME condition occurs. – mujahid Jan 27 '11 at 11:00
  • 3
    @mujahid: Then Open it normally UNTIL that condition happens, at which point you can open it with the `w` or `wb` flag. If you like you can close it immediately, then as your code executes normally open it for append. – Binary Worrier Jan 27 '11 at 11:02
  • 11
    @mujahid: No need to shout, we can hear you :) – rjnilsson Jan 27 '11 at 11:27
6

There are two ways:

1. fd=open(filename,O_RDONLY | O_WRONLY | O_TRUNC);


2. [cat /dev/null > filename] for BASH. It can be directly used in c program using [system()] system call. 

   system("cat /dev/null > filename");   
garvitlnmiit
  • 77
  • 1
  • 3
  • FYI: the second one is Bourne shell, not C. – michaelb958--GoFundMonica Jun 24 '13 at 10:36
  • Also, second one can be replaced with a single call to `truncate()`, which replaces the useless cat. – Hasturkun Jun 24 '13 at 11:57
  • 2
    From `man 2 open`: `Unlike the other values that can be specified in flags, the access mode values O_RDONLY, O_WRONLY, and O_RDWR do not specify individual bits. Rather, they define the low order two bits of flags, and are defined respectively as 0, 1, and 2. In other words, the combination O_RDONLY | O_WRONLY is a logical error, and certainly does not have the same meaning as O_RDWR.` So by specifying `O_RDONLY | O_WRONLY`, you are setting the low-order 2 bits to 3 instead of 2. The usage of 3 is implementation-defined and varies among systems. – Braden Best Feb 16 '17 at 19:42
2

I am using:

fp=freopen(NULL,"w",fp);
0

I prefer to use:-

system("echo "" > filename_with_complet_path");
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
SHASHI BHUSAN
  • 612
  • 6
  • 12