0

I want to read a binary file line by line (I'm writing of course continously, but I know that after 457 bytes new data start and I know exactly the byte structure and where which information is written to) and change a special entry of the line. I get an System.IO.IOException when I try to access the same file with both BinaryReader and BinaryWriter. I use locking to prevent that the file is accessed from somewhere else.

My code is:

using (FileStream fs2 = new FileStream(testfile, FileMode.Open, FileAccess.Read))
{
    using (BinaryReader r = new BinaryReader(fs2))
    {
        using (BinaryWriter bw = new BinaryWriter(new FileStream(testfile, FileMode.Open, FileAccess.Write), utf8))
        {
            for (int i = 0; i < 11000; i+=457)
            {
                int myint = r.ReadInt64();
                bw.Seek(i, SeekOrigin.Current);
                bw.Write(myint*2);
            }
        }
    }
}

How can I do this?

purbsel
  • 307
  • 8
  • 21
  • 1
    How can you read a binary file line by line? What do you mean by binary file in this context? What kind of binary file is it? – Peter Mortensen Oct 24 '19 at 12:20
  • 1
    How do you read a binary file _line by line_? Could you clarify what you are trying to achieve? – Martin Oct 24 '19 at 12:20
  • 2
    The code doesn't specify the FileShare to use, the default denies reading when the file is already opened for writing. The proper way to do this is with FileAccess.ReadWrite and let both BinaryReader and writer use the same stream. Note that FileShare.ReadWrite is too dangerous since it allows another process to mess with the file as well. – Hans Passant Oct 24 '19 at 12:20
  • 1
    It sounds like there is a duplicate somewhere. – Peter Mortensen Oct 24 '19 at 12:23
  • Could you open your file to read, open a different file to write to, then when you have finished whatever changes you want to make delete the original file and rename the new file? Or try this https://stackoverflow.com/questions/605685/how-to-both-read-and-write-a-file-in-c-sharp – ZedLepplin Oct 24 '19 at 13:25

2 Answers2

1

Do not create the second FileStream because the file is locked for the read operation by the first FileStream object.
If you are sure about file structure, the exception only can come out from 2nd FileStream instantiation. See link below for more information:
Read and Write to File at the same time

0

It is working for me using the following code:

if (File.Exists(testfile))
{
      FileInfo fi = new FileInfo(testfile);

      using (FileStream fs2 = new FileStream(testfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      {
          using (BinaryReader r = new BinaryReader(fs2))
          {
              r.BaseStream.Seek(0, SeekOrigin.Begin);                       
              using (BinaryWriter bw = new BinaryWriter(new FileStream(testfile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)))                        
              {
                  for (int i = 0; i <= (fi.Length-177); i += 177)//181
                  {
                  }
              }
          }
      }
}
purbsel
  • 307
  • 8
  • 21