Link to home
Start Free TrialLog in
Avatar of nearlyanexpert
nearlyanexpert

asked on

just want to empty a file...

Hi everybody...here goes:


fstream netfileio;

netfileio.open("C:\\dir1\\ex1.txt");

I know ex1.txt has things in it. All I want to do is totally empty the file.


Thanks guys,
NAE
Avatar of posternb
posternb

simply open the file for output (or use an ofstream)

netfileio.open("C:\\dir1\\ex1.txt",ios::out);

if you open a file for output and don't specify append mode it is immediately overwritten.
Avatar of nearlyanexpert

ASKER

Is there another way bceause there are things in the file that I want reading then I want to wipe the file.
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I know that I can do that but is there another way (no involving opening and closing files) as there will be over 3k files...
Then you are asking to do something system-dependent...  What platform are you on?
Windows...sorry, I wasn't aware it would make a difference :)
There is a Windows function DeleteFile, which takes a filename as a parameter and returns a BOOL (I believe) indicating success.  Check it out on msdn for more details, there is also a whole slew of other functions to copy, move, etc...
Noooo... :) I'm not wanting to delete the file, I'm wanting to clear it, as jhance said I can use:

1) Open the file for READ.
2) Read the stuff you want.
3) Close the file.
4) Open the file for WRITE.
5) Close it.

But I've got over 3k files, which will grow by large amounts, so I'm wanting a less file opening and closing as possible. I want to read the file in and I then want to clear the files contents.

NAE
Oops, I'm sorry I forgot -- duh.  Try CreateFile() instead then.  You can set the CREATE_ALWAYS flag to overwrite existing files though I wonder if this has any performance gain over open/closing the file...
Your could try using conio.h.  After the line you put insert:
Your could try using conio.h and iostream.h.  After the line you put insert:

          cout.flush();
          getch();

That will pause it until you press any key.  Then put this in to clear it.

          clrscr();
There is NO getting around opening and closing.  Sorry, but if you have designed a silly application that uses 3000 files, then you have only yourself to blame....

If you don't want to open each file 2X, I suppose you could OPEN, READ, CLOSE, DELETE, and then COPY a BLANK file to same name as the one you just deleted.
Yes, you do it the way I put and tell it to repeat the number of files you have and they will do it.
Are you answering THIS question or some other question?
THIS question, why?
open file
read contents
do whatever u want and be happy about it
no choice but to reopen the file in write mode


here are equivalent c calls
File*fp;
fp=fopen("file","r");
//do ur stuff
fp=freopen("file","w",fp); // no need to close . just reopen

//write whatever
fclose(fp);


Hmmm, how about this ?

char data[30000];
FILE *fp;
fp=fopen("filename.txt","r+w");
fread(data,sizeof(data),1,fp);
fseek(0, SEEK_SET);
fclose(fp);

Five instructions. Can't think the shorter one.
>>THIS question, why?

Because your comment is not at all applicable to this question.  At least that what it looks like to me.

Perhaps you should explain your meaning or re-read the question.  I just don't see how what you're saying applies at all.
Jhance, "Sorry, but if you have designed a silly application that uses 3000 files, then you have only yourself to blame...."

I wish it was a silly application! Unfortunately its massive, and this part of the program doesn't even touch the main application I've already got :) Each text file is already over 400k big, and as I've said, not only will that grow but the number of files will also! I just wondered if there was a way to clear a file without actually opening and closing it, but as there doesn't seem to be, I'll be a good sport and just accept the first comment that I would have done anyways :)

Thanks everybody for your response, congrats Jhance, another A...

Dude1337, keep trying...as Jhance said, your answer just didn't relate at all to what I wanted :)


Thanks again all,
NAE