removing a single string/line from a text file

This is a discussion on removing a single string/line from a text file within the C Programming forums, part of the General Programming Boards category; Hi I have a text file containing one string on each line. My problem is I want to surgically remove ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    13

    Question removing a single string/line from a text file

    Hi

    I have a text file containing one string on each line. My problem is I want to surgically remove just one line (or one string - same thing) without leaving a blank line.
    ie, if I had the following lines in my text file:
    thisisline1
    thisisline2
    thisisline3
    thisisline4

    I would like to get rid of, say, line 3, leaving:
    thisisline1
    thisisline2
    thisisline4

    I'm sure there must be an easy way to do this - a function hidden somewhere or something?! As far as I can tell, fscanf and fgets etc. can only copy the text, not 'cut' or remove it. I have scoured the man pages and searched the net and come up blank.

    If anyone could shed some light on this for me, it would be much appreciated.
    Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Quote Originally Posted by turmoil
    Hi

    I have a text file containing one string on each line. My problem is I want to surgically remove just one line (or one string - same thing) without leaving a blank line.
    ie, if I had the following lines in my text file:
    thisisline1
    thisisline2
    thisisline3
    thisisline4

    I would like to get rid of, say, line 3, leaving:
    thisisline1
    thisisline2
    thisisline4

    I'm sure there must be an easy way to do this - a function hidden somewhere or something?! As far as I can tell, fscanf and fgets etc. can only copy the text, not 'cut' or remove it. I have scoured the man pages and searched the net and come up blank.

    If anyone could shed some light on this for me, it would be much appreciated.
    Thanks.
    Nope, no hidden function (unless you write one yourself). Something like this requires you to bang out a solution yourself. Typically you would do something like this:
    Code:
    1.  Open the input file containing your lines of text.
    2.  Open an output file to write to.
    3.  Read a line from input file.
    4.  If this is the line you want to remove, don't write it to output file.
        Otherwise write the line to the output file.
    5.  While got more lines from input file go to step 4.
    6.  Close both files.
    7.  Delete the old file used as input.
    8.  Rename the output to have same name as old input file.
    If you were to store all the lines in a data structure such as a linked-list (or were perhaps inclined to use C++ for one of its handy container classes), then you could eliminate the deleting and renaming:
    Code:
    1.  Open input file.
    2.  Read all lines and store in your linked list.
    3.  Close input file.
    4.  Remove line you don't want from the linked list (or simply don't store it in the first place).
        (You could also store everything and then skip the writing of the line you don't want to
        end up in the file during the output step below).
    5.  Reopen the file previously used as input but now open it in write mode.
    6.  Output everything in linked-list to file (minus the line in question, however you
        may have dealt with that issue).
    7.  Close output file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    13
    Hey thanks man. I'm supprised it takes such an effort to achieve that result, but hey, if it works... Well, I know it works, I just tried it. :-)

    Thanks again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why would you be surprised? Just think of how a text editor works. You open a file. You change something. You save the file.

    Text editors don't directly interface with the disk. They load everything into memory, you change it, then it saves over top of the file.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Why would you be surprised?
    When you press delete at the beginning of a line, it does the same thing, but it appears to the user that one letter was removed and everything else automatically shifted back - you don't actually see all the data being written one column earlier.

    And for all the OP knows, there may be a standard function already written to do this when given a certain element number as a parameter.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which is entirely different than deleting a portion of the file. You haven't saved anything when you press the delete key. It's still in memory. Nothing saves unless you tell it to. Which means, it follows common sense that that's how you'd do it yourself. Load it up, edit it, save it.

    But I guess they don't teach logic, and you can't teach common sense.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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