Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

just want to empty a file...

Posted on 2003-02-27
20
Medium Priority
?
224 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

580 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