0

Hey there,

So I've been searching for method to replcace on particular line in an UTF-8 (easily readable and copy-paste) file.
I found lots of methods that were searching for matches in rows and replacing them.
It was mentioned very often that it's impossible to replace a row in text file using C#, unless they're "accidentally" same size.
Mine will be, it will be a file with constant length 200~300 of on each row, repeating vast amount of times 2000~???.

Could someone please provide me (link with) script that would replace row 12 to for example PLPLPPLPLPL (this test file will be just that short), so I can evaluate the script and modify it to my purposes and most likely learn from it. Problem is, it needs to be able to replace the row regardless what's on it. The length of each and single one row will be exactly same.

2
Contributors
2
Replies
24
Views
3 Years
Discussion Span
Last Post by ddanbe
0

Oh, I forgot to mention, there are possbilities to do that. But what they do is entirely read the whole file, replace the row in an array, and then write it, but it would be big anti-boost down to performance, given that the file would have to be used multiple number of times.

1

Use the FileStream.Seek method to position yourself on the line to be removed. Multiply your linelength with the lineNr to be removed minus one to postion yourself in the file just before the line to be removed. Now write the new line.
Success.

Votes + Comments
If every single answer was given like that, I'd be C# professional by now! Great answer, gonna try it.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.