CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    141

    remove the last line from StreamReader

    hi...

    how do we remove the last line of the file we open in StreamReader? and then we saved the edited file (without the last line). is there any way to do it?

    any help is greatly appreciated. thanks a lot
    Last edited by imin; August 20th, 2007 at 10:51 PM.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: remove the last line from StreamReader

    There's the smart way:

    Open the file using a FileStream, seek to the end of the file, then start scanning backwards byte by byte until you come across a newline character (meaning you have a /n with a /r before it). Then you just set the length of the file equal to the current position which should truncate the file so the last line is gone.


    Then there's the easy way:

    Open the file using a file stream. Read it in line by line and write out each line into a new file. You put in logic so that you don't copy the last line into the new file. Then just close both filestreams and delete the original file and rename the new file to the same name as the old flie.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Dec 2006
    Posts
    203

    Re: remove the last line from StreamReader

    Quote Originally Posted by Mutant_Fruit
    There's the smart way:

    Open the file using a FileStream, seek to the end of the file, then start scanning backwards byte by byte until you come across a newline character (meaning you have a /n with a /r before it). Then you just set the length of the file equal to the current position which should truncate the file so the last line is gone.


    Then there's the easy way:

    Open the file using a file stream. Read it in line by line and write out each line into a new file. You put in logic so that you don't copy the last line into the new file. Then just close both filestreams and delete the original file and rename the new file to the same name as the old flie.
    I'd agree to this, although I'd probably use the easy way with the exception that I'd load the entire file into a string, close the file, shorten the string accordingly, and then overwrite the old file with the new string.
    Sincerely,

    Martin Svendsen

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: remove the last line from StreamReader

    Quote Originally Posted by Homogenn
    I'd agree to this, although I'd probably use the easy way with the exception that I'd load the entire file into a string, close the file, shorten the string accordingly, and then overwrite the old file with the new string.
    I just hope it's not a large file then.

    *tries to imagine loading a 500 megabyte string in memory*
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Dec 2006
    Posts
    203

    Re: remove the last line from StreamReader

    Quote Originally Posted by Mutant_Fruit
    I just hope it's not a large file then.

    *tries to imagine loading a 500 megabyte string in memory*
    Haha, yeah, okay, if it's a big file, I'd probably do it the smart way, anyway
    Sincerely,

    Martin Svendsen

  6. #6
    Join Date
    Jul 2005
    Posts
    141

    Re: remove the last line from StreamReader

    thanks a lot for the help... i've done it using the easy way you proposed.. thanks again

    but now i've another problem.. i will open a new thread about it... hope you all can help again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured