Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think that (a) is the best because:</p> <ul> <li>Current Position will be incremented as you read and you don't need to worry about to store it somewhere;</li> <li>You don't need to open it and seek required position (it shouldn't be much slower to reopen but keeping it open gives OS some hints for optimization I believe) each time you poll it;</li> <li>Other solutions I can think out requires PInvokes to system interprocess synchronisation primitives. And they won't be faster than file operations already in framework.</li> </ul> <p>You just need to set proper FileShare flags:</p> <p>Just for example:</p> <p><strong>Server:</strong></p> <pre><code>using(var writer = new BinaryWriter(new FileStream(@"D:\testlog.log", FileMode.Append, FileAccess.Write, FileShare.Read))) { int n; while(Int32.TryParse(Console.ReadLine(), out n)) { writer.Write(n); writer.Flush(); // write cached bytes to file } } </code></pre> <p><strong>Client:</strong> </p> <pre><code>using (var reader = new BinaryReader(new FileStream(@"D:\testlog.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { string s; while (Console.ReadLine() != "exit") { // allocate buffer for new ints Int32[] buffer = new Int32[(reader.BaseStream.Length - reader.BaseStream.Position) / sizeof(Int32)]; Console.WriteLine("Stream length: {0}", reader.BaseStream.Length); Console.Write("Ints read: "); for (int i = 0; i &lt; buffer.Length; i++) { buffer[i] = reader.ReadInt32(); Console.Write((i == 0 ? "" : ", ") + buffer[i].ToString()); } Console.WriteLine(); } } </code></pre>
 

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