Click here to Skip to main content
16,017,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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().
Posted
Comments
CHill60 10-Apr-13 6:54am    
can't remember the syntax off the top of my head, but isn't there is an option when you open the file to open for write (which effectively clears the contents) rather than append - see http://www.cprogramming.com/tutorial/cfileio.html

Don't bother, just use seek to return the file pointer to the beggining of the file and then write an EOF if you want to.
On Win32 there's an API to set the extent of the file which you could use to prevent anything reading past that EOF even if it chose to. That's not necessary though if your code is in full control of the file.
 
Share this answer
 
Comments
ayesha hassan 10-Apr-13 7:13am    
fpFile = fopen("sendclInformation.txt", "r+");
fseek(fpFile, 0, SEEK_SET);
WriteFile(file,EOF,strlen(EOF),&dwWritten,0);

I tried this but:
error C2664: 'strlen' : cannot convert parameter 1 from 'int' to 'const char *'
Matthew Faithfull 10-Apr-13 7:18am    
You need to open with write permission to start with then don't use strlen with EOF, EOF is numeric constant the size of a TCHAR so use something like:
WriteFile( file, EOF, sizeof(TCHAR), &dwWritten, 0 );
ayesha hassan 10-Apr-13 7:21am    
and now the error:
error C2664: 'WriteFile' : cannot convert parameter 2 from 'int' to 'LPCVOID'
Matthew Faithfull 10-Apr-13 7:36am    
My bad I forget the signature for WriteFile, change EOF for reinterpret_cast< const void*>(EOF) . Messy but should work.
open the file with stream reader then write nothing
like:-

Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)

objFile.Write ""
objFile.Close
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900