removing text from a file

This is a discussion on removing text from a file within the C++ Programming forums, part of the General Programming Boards category; ok the answer to this is probably very simple, but i've never had to do it before and had no ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    removing text from a file

    ok the answer to this is probably very simple, but i've never had to do it before and had no luck on google, so maybe someone here can help me. i have a .txt file that i'm writing to, using an ofstream, and i want to be able to delete a line from the file. how can i do this?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Read each line into a linked list or even just an array (if you count the lines first). Then loop thru the linked list/array and output only the lines you want to a file.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It's a common misconception that you can write, "part", of a file. What actually needs to be done, even if the concept in your mind is that of simpling adding or removing a line, is that every byte needs to be read into memory, edited by your program (often the new data is just read into a new array in memory), and then every byte is written back to the program.

  4. #4
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    Put every line into a vector, then find and remove the line you wish to delete, then iterate through the vector and place the lines back into the file.
    Bagpipes putting the fun back in funeral.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    thanks

    thanks for the help guys. much appreciated.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Why would you want to read the whole file into memory?
    Surely just read each line, and write it out if you want to keep it, is much more efficient in memory terms.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21