Menu

Overwrite or delete characters from fstreams?

2006-02-07
2012-09-26
  • Nobody/Anonymous

    Is it possible to literally take a character out of a stream? In my experience, using ifstream::get() only gets a copy of the value at the current point in the file, but what I want to do is rip the character out of the file. In a sense, what I'm asking is if it's possible to write in the middle of a file without having to clear the file, write the first part (which is the same as it was), write the different part, and write the ending (which is the same as it was)? I sure hope not as this would be very inconvenient when writing a whole lot of data to a flash drive.

     
    • Githlar

      Githlar - 2006-02-12

      OK, I've gotten it to overwrite the characters, but I still haven't got it to take characters out of a stream. For example, I have a file that contains only the word "only". If I wanted to change that word to "ony", how would I do that without rewriting the file? I've tried fstream::seekp()ing to the spot and then writing "" to the file and writing a backspace to the file, neither of which works.

       
      • Nobody/Anonymous

        you cant do that using the ANSI standard C/C++ libraries. you can memmap the file and go that root. which means not portability. you can copy the file like you know. which means creating a temp and deleteing an old file. you can use two file pointers (or streams) to the same file and copy the data in parallel omminting what you dont want on the fly. the problem with the last approach is that when you decrease the size of the file using standard libraries you will have junk at the end beause you can not trim a file using standard libraries. you can using posix.

        one way or the other you are going to copy data. memmapped files copy the data backto the harddrive after mapping it into address space.

        heres why... no matter what you write to a file it either replaces data in the file or appends to the file. no matter what you read from a file it does not remove or modify the file in any way... it only increments the pointer to the current read position. writting a backspace is the literally writting the character code for backspace to the file.

        the most portable way to do this is to use fstreams in binary mode accounting for line endings yourself and inherit from streambuf and fstream so that you can have a memmapped file pretending to be a stream like cout/cin.

         
    • Nobody/Anonymous

      copying the first part is the way to go for standard c/c++.

       
    • Nobody/Anonymous

      So, there's no way to do it without consuming ungodly amounts of memory or time?

       
    • Nobody/Anonymous

      There's no way to in effect insert characters into a stream and then delete the character ahead of it (which would be the character that it is replacing)?

       
    • Nobody/Anonymous

      ah. i may have misunderstood your question. yes. you can overwrite characters. in fact that is the default. tell us how you are opening the stream and how you are operating on it and we will tell you what to change. (an working full example of what you are doing is best. we dont need alot just what you are doing)

       
    • Githlar

      Githlar - 2006-02-12

      Well, I figured out how to overwrite files. I found that you have to open the file to overwrite as both ios_base::in and ios_base::out in order to evoke this behavior.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.