Reputation: 2754
I am working on a sever/client applicataion. I want to maintain information of all active clients in a text file named "Information.txt". I update this text file after every 3 seonds. So, I want the text file to clear all of its contents after every 3 seconds without deleting the file. Is there any way to do it ? :( I don't want to use freopen().
Upvotes: 1
Views: 906
Reputation: 6568
Just open the file with fopen
and setting the flag to w or w+ or wb
From fopen man page
w
Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+
Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
Upvotes: 1
Reputation: 726579
A problem with clearing the file periodically is that if your process crashes after the file has been cleared but before it has been written, you lose data: the old data is gone, but the new data is not there yet.
A common approach to this problem is to create a new file, writing it, and then moving the new file to replace the old one. This way you always have a file, and sometimes (for very brief periods of time) you have two files.
Upvotes: 3
Reputation: 10553
Try with
fopen(filename, flag)
Open your file with flag= "w" or "wb" and it will be cleared
Upvotes: 1