Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deleting data from the file without deleting the file first

Reply
Thread Tools

Deleting data from the file without deleting the file first

 
 
crea
Guest
Posts: n/a
 
      12-27-2012
This is a simple question, but I have never found an answer to this and been
thinking about it. Plus not sure if this is the right forum, but not sure
where to put this...I guess some kind of general programming guestion.

So we open a file with iofstream and then write /save some text to file.
Lets say we save a text:

"Hello World!!"

To a file HW.txt.

Now, is it possible to delete the character 'o' from the file without
deleting the file HW.txt first?
Currently I think the only way to do this is to read the whole data from
file to a string and then delete it from the string and then save the whole
string to the file.



 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      12-27-2012
On Thu, 27 Dec 2012 07:58:28 +0000, crea wrote:

> Lets say we save a text:
>
> "Hello World!!"
>
> To a file HW.txt.
>
> Now, is it possible to delete the character 'o' from the file without
> deleting the file HW.txt first?


That depends on what you mean with 'delete the file'. You have to rewrite
the entire file, but you don't have to tell the filesystem that the file
is gone before you write it anew.

> Currently I think the only way to do this is to read the whole data from
> file to a string and then delete it from the string and then save the
> whole string to the file.


Yes, that is correct. Note that those steps don't include removing the
file from the filesystem. When you open the file for writing/saving, you
just have to use ios:ut|ios::trunc to ensure the old contents are gone.

Bart v Ingen Schenau
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      12-28-2012
On Thu, 27 Dec 2012 07:58:28 +0000, crea wrote:

> So we open a file with iofstream and then write /save some text to file.
> Lets say we save a text:
>
> "Hello World!!"
>
> To a file HW.txt.
>
> Now, is it possible to delete the character 'o' from the file without
> deleting the file HW.txt first?


Using only the features of the standard C or C++ libraries, the best you
can do is to shift everything after the "o" back one space, by reading it
in then writing it out at a different offset.

Some operating systems may have the ability to delete chunks from the
middle of the file, but most don't, and the standard library doesn't
provide any mechanism to make use of such a feature.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting first N lines from a text file pozz C Programming 26 12-16-2011 07:05 AM
Open file, get first line, delete first line close file Richard Schneeman Ruby 16 08-26-2008 11:54 PM
deleting first line from a file suresh Ruby 15 06-05-2008 10:33 AM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
help with my first project on first job, how to read a strange file, thanks a lot!!!!!!! matt Java 9 10-27-2004 03:32 AM



Advertisments