?
Solved

just want to empty a file...

Posted on 2003-02-27
20
Medium Priority
?
223 Views
Last Modified: 2010-07-27
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
0
Comment
Question by:nearlyanexpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +3
20 Comments
 

Expert Comment

by:posternb
ID: 8036407
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.
0
 

Author Comment

by:nearlyanexpert
ID: 8036426
Is there another way bceause there are things in the file that I want reading then I want to wipe the file.
0
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 8036576
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nearlyanexpert
ID: 8036607
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...
0
 

Expert Comment

by:posternb
ID: 8036624
Then you are asking to do something system-dependent...  What platform are you on?
0
 

Author Comment

by:nearlyanexpert
ID: 8036643
Windows...sorry, I wasn't aware it would make a difference :)
0
 

Expert Comment

by:posternb
ID: 8037598
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...
0
 

Author Comment

by:nearlyanexpert
ID: 8037655
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
0
 

Expert Comment

by:posternb
ID: 8037907
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...
0
 

Expert Comment

by:dude1337
ID: 8038349
Your could try using conio.h.  After the line you put insert:
0
 

Expert Comment

by:dude1337
ID: 8038360
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();
0
 
LVL 32

Expert Comment

by:jhance
ID: 8038660
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.
0
 

Expert Comment

by:dude1337
ID: 8038704
Yes, you do it the way I put and tell it to repeat the number of files you have and they will do it.
0
 
LVL 32

Expert Comment

by:jhance
ID: 8038720
Are you answering THIS question or some other question?
0
 

Expert Comment

by:dude1337
ID: 8038766
THIS question, why?
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8038893
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);


0
 
LVL 5

Expert Comment

by:Kocil
ID: 8040342
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.
0
 
LVL 32

Expert Comment

by:jhance
ID: 8040959
>>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.
0
 

Author Comment

by:nearlyanexpert
ID: 8042057
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
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question