Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 ? Frown | :( I don't want to use freopen().
Posted 10-Apr-13 1:49am
Comments
CHill60 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
ayesha hassan at 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 at 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 at 10-Apr-13 7:21am
   
and now the error:
error C2664: 'WriteFile' : cannot convert parameter 2 from 'int' to 'LPCVOID'
Matthew Faithfull at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100