Click here to Skip to main content
11,928,987 members (52,867 online)
Rate this:
Please Sign up or sign in to vote.
See more: C Windows file Text
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
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
Rate this: bad
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.
ayesha hassan 10-Apr-13 7:13am
fpFile = fopen("sendclInformation.txt", "r+");
fseek(fpFile, 0, SEEK_SET);

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

open the file with stream reader then write nothing

Const ForWriting = 2

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

objFile.Write ""

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2015
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