Reputation: 29
I have saved data using BinaryWriter
in a file.
Now I want to read the data from that file (starting at a particular position) using BinaryReader
.
While writing to the file, I have added a string 'BREAK' in the middle of the file.
I want to read from 0 to before 'BREAK' and after 'BREAK' to end OF FILE in C#. Please help me.
Upvotes: 2
Views: 2473
Reputation: 3430
BinaryWriter
is used for writing a NON-human-readable format hence, binary. Example, if you want to write primitive types, you can just write them in their byte representation (like boolean)
So for writing/reading a human-readable file, you can just use a StreamReader
/StreamWriter
. It would be much easier.
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 StreamWriter
. But the code below uses BinaryWriter
.
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);
}
}
}
But if you really insist on using BinaryReader
and BinaryWriter
here's one solution.
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<byte> byteList = new List<byte>();
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 < 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] && i > 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);
}
}
}
Upvotes: 1
Reputation: 3131
BinaryReader reads byte by byte or chars. You will have to read every char and try to check for the combination you are looking for. If found "B", check if next is "R" if so, then check if next is "E" and so on. There might also be some good algo available for this.
Upvotes: 0