Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>BinaryWriter</code> is used for writing a NON-human-readable format hence, <em>binary</em>. Example, if you want to write primitive types, <a href="http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx#remarksToggle" rel="nofollow">you can just write them in their byte representation (like boolean)</a></p> <p>So for writing/reading a human-readable file, you can just use a <code>StreamReader</code>/<code>StreamWriter</code>. It would be <strong>much easier</strong>.</p> <h2>Using StreamReader</h2> <p>The snippet below works as per your specification, but it's just a guide if you want to improve its algorithm or whatsoever. I also strongly suggest that you just use a <code>StreamWriter</code>. But the code below uses <code>BinaryWriter</code>.</p> <pre><code>namespace _16953330 { class Program { static void Main(string[] args) { string input = @"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. fox jumps over the lazy dfox jumps over the lazy dBREAK fox jumps over the lazy dfox jumps over the lazy d The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."; using (var stream = new FileStream("file.txt", FileMode.OpenOrCreate)) { using (var writer = new BinaryWriter(stream)) //why use a BinaryWriter if you're gonna write in a human-readable format? { writer.Write(input); } } string firstPart = string.Empty; string secondPart = string.Empty; StringBuilder sb = new StringBuilder(); using (var stream = new FileStream("file.txt", FileMode.OpenOrCreate)) { using (var reader = new StreamReader(stream)) { string line; while (!string.IsNullOrEmpty(line = reader.ReadLine())) { int indexOfBreak = line.IndexOf("BREAK"); if (indexOfBreak == -1) { sb.Append(line); } else { string untilBeforeBREAK = line.Substring(0, indexOfBreak); sb.Append(untilBeforeBREAK); //remove the first 2 characters in the file as the BinaryWriter //writes the length of the bytes should a BinaryReader expect //(if ever you want to use a binary reader which I don't understand //because you are reading a HUMAN READABLE file) firstPart = sb.Remove(0,2).ToString(); sb.Clear(); string breakUpToTheEnd = string.Empty; sb.Append(line.Substring(indexOfBreak)); sb.Append(reader.ReadToEnd()); secondPart = sb.ToString(); } } } } Console.WriteLine(firstPart); Console.WriteLine(Environment.NewLine); Console.WriteLine(secondPart); } } } </code></pre> <h2>Using BinaryReader</h2> <p>But if you really insist on using <code>BinaryReader</code> and <code>BinaryWriter</code> here's one solution.</p> <pre><code>namespace _16953330 { class Program { static void Main(string[] args) { byte[] input = Encoding.ASCII.GetBytes( @"I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryBREAKWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. I have saved data using BinaryWritter in a file. "); //just for sampling purposes, considering that what you write is NON-human-readable using (var stream = new FileStream("somefile.exe", FileMode.OpenOrCreate)) { using (var writer = new BinaryWriter(stream)) { writer.Write(input); } } byte[] firstPart = null; byte[] secondPart = null; StringBuilder sb = new StringBuilder(); string breaker = "BREAK"; List&lt;byte&gt; byteList = new List&lt;byte&gt;(); using (var stream = new FileStream("somefile.exe", FileMode.OpenOrCreate)) { using (var reader = new BinaryReader(stream)) { while (reader.BaseStream.Position != reader.BaseStream.Length) { byte currentByte; currentByte = reader.ReadByte(); if (currentByte == 'B') { byteList.Add(currentByte); for (int i = 1; i &lt; breaker.Length; i++) { currentByte = reader.ReadByte(); byteList.Add(currentByte); //if the succeeding characters match the characters after B in BREAK if ((char)currentByte == breaker[i] &amp;&amp; i &gt; 1) { byteList.RemoveAt(byteList.Count - 1); if (i == breaker.Length - 1) { //if the for loop reaches its end and matches all characters in BREAK //remove B (which was added above) and K (added last) byteList.RemoveAt(byteList.Count - 1); byteList.RemoveAt(byteList.Count - 1); firstPart = byteList.ToArray(); byteList.Clear(); } } } } else { byteList.Add(currentByte); } } secondPart = byteList.ToArray(); } } Console.WriteLine(firstPart); Console.WriteLine(Environment.NewLine); Console.WriteLine(secondPart); } } } </code></pre>
    singulars
    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.
 

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