Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Memory Mapped Files can be a problem for big files (typically files that are of a size equivalent or bigger than the RAM), in case you eventually map the whole file. If you map only the end, that should not be a real issue.</p> <p>Anyway, here is a C# implementation that does not use Memory Mapped File, but a regular FileStream. It is based on a <code>ReverseStreamReader</code> implementation (code also included). I would be curious to see it compared to other MMF solutions in terms of performance and memory consumption.</p> <pre><code>public static void OverwriteEndLines(string filePath, int linesToStrip) { if (filePath == null) throw new ArgumentNullException("filePath"); if (linesToStrip &lt;= 0) return; using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) { using (ReverseStreamReader reader = new ReverseStreamReader(file)) { int count = 0; do { string line = reader.ReadLine(); if (line == null) // end of file break; count++; if (count == linesToStrip) { // write CR LF for (int i = 0; i &lt; linesToStrip; i++) { file.WriteByte((byte)'\r'); file.WriteByte((byte)'\n'); } // truncate file to current stream position file.SetLength(file.Position); break; } } while (true); } } } // NOTE: we have not implemented all ReadXXX methods public class ReverseStreamReader : StreamReader { private bool _returnEmptyLine; public ReverseStreamReader(Stream stream) : base(stream) { BaseStream.Seek(0, SeekOrigin.End); } public override int Read() { if (BaseStream.Position == 0) return -1; BaseStream.Seek(-1, SeekOrigin.Current); int i = BaseStream.ReadByte(); BaseStream.Seek(-1, SeekOrigin.Current); return i; } public override string ReadLine() { if (BaseStream.Position == 0) { if (_returnEmptyLine) { _returnEmptyLine = false; return string.Empty; } return null; } int read; StringBuilder sb = new StringBuilder(); while((read = Read()) &gt;= 0) { if (read == '\n') { read = Read(); // supports windows &amp; unix format if ((read &gt; 0) &amp;&amp; (read != '\r')) { BaseStream.Position++; } else if (BaseStream.Position == 0) { // handle the special empty first line case _returnEmptyLine = true; } break; } sb.Append((char)read); } // reverse string. Note this is optional if we don't really need string content if (sb.Length &gt; 1) { char[] array = new char[sb.Length]; sb.CopyTo(0, array, 0, array.Length); Array.Reverse(array); return new string(array); } return sb.ToString(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload