Pages

Saturday, September 14, 2019

C# .net file Prepend

In C# .net, there are no built-in feature for file prepend . However, if we want to write a file reversely we can read the lines from backward and write usually in a new file. this will generate a reverse copy of the main file.

We can read reversely like https://www.blakepell.com/2010-11-29-backward-file-reader-vb-csharp-source  - only issue is the the line feed and carriage return characters need to give a backslash (\) in the code.

If we try to write using FileStream and use Seek(0, SeekOrigin.Begin) to position write cursor at the begin ( or in a random position manipulating the offset and SeekOrigin) the contents will be overwritten.

We can use the insertion sort's trick then, we copy-shift the contents down upto the size of new content's size and write to the determined positon, it will overwrite the repeated contents created due to our "copy-shift" operation




So you understand the pain point for a large file of this raw technique.

No comments :

Post a Comment