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
C++

Avatar of undefined
Last Comment
nearlyanexpert

8/22/2022 - Mon
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.
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
jhance

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
nearlyanexpert

ASKER
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...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
posternb

Then you are asking to do something system-dependent...  What platform are you on?
nearlyanexpert

ASKER
Windows...sorry, I wasn't aware it would make a difference :)
posternb

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...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
posternb

nearlyanexpert

ASKER
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
posternb

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...
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
dude1337

Your could try using conio.h.  After the line you put insert:
dude1337

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();
jhance

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dude1337

Yes, you do it the way I put and tell it to repeat the number of files you have and they will do it.
jhance

Are you answering THIS question or some other question?
dude1337

THIS question, why?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
akshayxx

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);


Kocil

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.
jhance

>>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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
nearlyanexpert

ASKER
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