Question

I have binary file

BinaryWriter binwriter = new BinaryWriter(File.Open("C:\\temp\\Users.bin", FileMode.Create));
binwriter.Write(buff);
binwriter.Close();

It works, but how can I read data from this file? I need to read new line each time, while it is not end of file.

BinaryReader binreader = new BinaryReader(File.Open("C:\\temp\\Users.bin", FileMode.Open));
byte[] m = binreader.ReadBytes(??????); //I to read only 1 line to m, and then I need to read again new line to m. 
Was it helpful?

Solution

Binary file doesn't have the concept of a "line", however you can try to read it like a text file by doing this way :

  using (var streamReader = new StreamReader(filePath))
  {
    string line;
    while ((line = streamReader.ReadLine()) != null)
    {
      Console.WriteLine(line);
    }
  }

OTHER TIPS

using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }

you can of course adapt it to your needs instead of printing it on the Console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top