Removing the last line of a file.

This is a discussion on Removing the last line of a file. within the C++ Programming forums, part of the General Programming Boards category; I need to remove the last line of a text file. The last line is always the same, but the ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    Removing the last line of a file.

    I need to remove the last line of a text file. The last line is always the same, but the files could be very large so copying the file to a container and writing out all lines but the last is not an option.

    Is there a quick way to do this? All of my other file code is using ofstreams, but I don't much care what objects I have to use to remove the line since it will be an action seperate from the rest of the file operations.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The last line is always the same
    So ignore it...
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Originally posted by Prelude
    >The last line is always the same
    So ignore it...
    Sorry, I should have been more specific. I need to remove the last line and then continue to add output to the file.

    Basically, I am writing an XML file and I have to write the closing tag everytime I close the file. Then to add to the file I have to erase the closing tag, add the new info and rewrite the closing tag.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try this:
    Code:
    open file
    create/open new 'temp' file
    read line
    output line to new 'temp' file
    if line==last line
         output new data to 'temp' file
    output last line (the last line you read in) to 'temp' file
    close both files
    copy 'temp' file to old file
    delete 'temp' file
    this way your not copying the whole file, into free-store... only line-by-line

    note: if you do it this way, make sure the 'temp' file is clear of everything before you start writing to it (ios::trunc) because it will screw up your program eternally if you don't watch the XML file
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    That is a pretty good temperary solution major_small. Thanks. I am still holding out hope that there is a way to do it without going through the entire file.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you mean not rewriting the entire file? there probably is, but it's not as easy

    yes, there is such a thing as a lazy coder.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    How about this?
    Code:
    fileObject.seekg(n, ios::end);
    When n = 0, the get pointer will be placed at the end of fileObject; when n = 100, it means 100 bytes back from end of fileObject. I don't know how you could apply it for your purpose. Anyways, the same operation can be done with seekp (put pointer).

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    3
    Find out how many bytes the closing tag is and use the seekg(that many bytes, ios::end) option, then just start writing.

    I think that will work.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Originally posted by DeathNinja42
    Find out how many bytes the closing tag is and use the seekg(that many bytes, ios::end) option, then just start writing.

    I think that will work.
    That is what I am trying now, it doesn't seem to be working, no matter how far back I seek, it just starts to write at the end of the file. I believe the problem is that I was opening the file in ios::app mode, which I read resets the seek pointer to the end of the file before each inser. I am now trying to open in ios::ate, but this seems to be overwriting the whole file for some reason.

    In any case, this is all very helpful. Thanks to everyone who responded. I'll keep working on it.

    <edit> but it should be seekp and not seekg, right? </edit>

  10. #10
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I can't tell what you did wrong but there is seekg() and there is seekp() . The first one is for the istream which at which the next input is to occur. The latter is for ostream which indicates the byte number in the file at which the next output should be placed.
    n your case, you want to use seekp

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Thanks

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    However, for true commit or rollback behavior, you really want to make a copy, modify the copy, and only if everything else succeeded 100% do you delete the original and rename the copy.

    It's a better strategy.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 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. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM

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