Reputation: 101
I'm coding on Unix. I've a text file as output. How can I overwrite the content every time I call a function?
Example:
functionA(){
fp = fopen("text.txt",someflag);
...
...blabla
....
fprintf(fp,"some new text");
}
So by calling functionA
, I should have text.txt with new content.
For someflag
, I've already tried a
, ab
, and w+
, but none with the result I want. (ab
appends new text, w+
open the file, but fprintf
doesn't work).
I'm using C on unix.
Upvotes: 1
Views: 6664
Reputation: 9259
Flag "w"
should overwrite the existing file.
See this documentation for details, specifically:
The mode argument points to a string. If the string is one of the following, the file shall be opened in the indicated mode. Otherwise, the behavior is undefined.
- r or rb: Open file for reading.
- w or wb: Truncate to zero length or create file for writing.
- a or ab: Append; open or create file for writing at end-of-file.
- r+ or rb+ or r+b: Open file for update (reading and writing).
- w+ or wb+ or w+b: Truncate to zero length or create file for update.
- a+ or ab+ or a+b: Append; open or create file for update, writing at end-of-file.
Upvotes: 5