Thread: Deleting Text from a Text file

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So please could some one point me in the right direction.
    Prelude gave you the steps already. Is there something specific you are having trouble implementing?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Deleting Text from a Text file

    Hello everybody

    I am new to c++ and I am trying to find a way to delete a line of text from a text file.

    My program is writing to the file no problem and I can read it back in and display it in a DOS console screen. But one of the options I need is to find a line of text and then delete that line from the file.

    Is C++ capable of doing such a thing?

    plus how do you search a text file for a specific word or words?

    Thank you for any help you may provide.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is C++ capable of doing such a thing?
    No. I say no because when people ask if C++ is "capable" of doing something, they want a simple function like:
    Code:
    #include <magic>
    
    bool delete_line_from_file ( fstream& file, int line_number );
    Can you do it? Yes. Does C++ give you the tools to do it? Yes. Is everything you could ever want wrapped up in a nice little package with a bow? Of course not. To delete a line from a file, the common solution is to do something like read the file into a vector, delete the line you want to delete, and then overwrite the file with your changes because most files don't support record-oriented operations like deletion of a line.

    >plus how do you search a text file for a specific word or words?
    The string class has a slew of find member functions that will help you pick out pieces.
    My best code is written with the delete key.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Is C++ capable of doing such a thing?
    C++ is capable of doing almost anything. (Unless you want to do something unrealistic)
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    Ok then I understand C++ is capable of doing such a task

    And I understand that it will not be easy to do.

    I know it won't be an easy thing to do.

    If it was just a simple one line command then why would I be asking
    a forum how to do it.

    So please could some one point me in the right direction.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    * Read in a file to some variable, a vector of strings was suggested.
    * Decide what you want to be taken out from the file, ie a line number or something
    * Delete that from memory (from the variable).
    * Save the variable's contents to the same file name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM