|
-
August 20th, 2007, 10:48 PM
#1
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.
-
August 20th, 2007, 11:27 PM
#2
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.
-
August 21st, 2007, 02:06 AM
#3
Re: remove the last line from StreamReader
 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
-
August 21st, 2007, 07:33 AM
#4
Re: remove the last line from StreamReader
 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.
-
August 21st, 2007, 08:53 AM
#5
Re: remove the last line from StreamReader
 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
-
August 23rd, 2007, 07:50 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|