2

I have read a binary file in using C# as per the following code. Then I tried to write this binary data in another binary file. But I found that when I opened these 2 files in Winmerge, there is a difference in both binary files. i.e read file and written file. Could you please suggest why there is a difference if I just read files and rewrite?

       string fileNameWithPath_ = "1.pwpmi";
       string newfileNameWithPath_ = "2.pwpmi";

        System.IO.FileStream fileStream = new System.IO.FileStream(fileNameWithPath_, System.IO.FileMode.Open,
            System.IO.FileAccess.Read);
        System.IO.BinaryReader binReader = new System.IO.BinaryReader(fileStream, Encoding.ASCII);


        char[] chararr = new char[fileStream.Length];

        chararr = binReader.ReadChars((int)fileStream.Length);
        byte[] buffer = binReader.ReadBytes((int)fileStream.Length);

        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes,0, (int)fileStream.Length);

        byte[] fileBytes = System.IO.File.ReadAllBytes(fileNameWithPath_);
        string stringbyte1 = Encoding.ASCII.GetString(fileBytes);

        binReader.Close();
        fileStream.Close();
        System.IO.BinaryWriter binWriter =
        new System.IO.BinaryWriter(System.IO.File.Open(newfileNameWithPath_, System.IO.FileMode.Create));
        binWriter.Flush();
        binWriter.Write(stringbyte1);
        binWriter.Close();
Ganesh Chavan
  • 57
  • 1
  • 1
  • 6
  • 1
    The only piece of this code that can be saved is perhaps `System.IO.FileStream fileStream = new System.IO.FileStream(...)`. Can you describe what you're actually trying to do? Do you simply want to create a copy of a file? Or change some parts of the first, saving the result in the second? Something else? – Jimi May 18 '20 at 03:41
  • Why are you making this harder than just `File.Copy(srcPath, dstPath)` or at worst (if you actually want to intercept or transform the data you copied): `File.WriteAllBytes(dstPath, File.ReadAllBytes(srcPath))`? – Wyck May 18 '20 at 04:19

2 Answers2

3

It appears you have tried a few different methods and have actually come quite close to the working one. The issue is likely in the way your read your binary data as one data type and write it back into output as another. Try sticking to bytes:

    string fileNameWithPath_ = "1.pwpmi";
    string newfileNameWithPath_ = "2.pwpmi";

    System.IO.FileStream fileStream = new System.IO.FileStream(fileNameWithPath_, System.IO.FileMode.Open,
        System.IO.FileAccess.Read);
    System.IO.BinaryReader binReader = new System.IO.BinaryReader(fileStream, Encoding.ASCII);
    byte[] fileBytes = binReader.ReadBytes((int)fileStream.Length);
    //byte[] fileBytes = System.IO.File.ReadAllBytes(fileNameWithPath_); // this also works

    binReader.Close();
    fileStream.Close();
    System.IO.BinaryWriter binWriter =
    new System.IO.BinaryWriter(System.IO.File.Open(newfileNameWithPath_, System.IO.FileMode.Create));
    binWriter.Flush();
    binWriter.Write(fileBytes); // just feed it the contents verbatim
    binWriter.Close();

the above code does not make any changes to the incoming byte stream and produces identical files when I run it through WinMerge

As comments suggest, you might be better off just copying the file altogether:

    string fileNameWithPath_ = "1.pwpmi";
    string newfileNameWithPath_ = "2.pwpmi";
    File.Copy(fileNameWithPath_, newfileNameWithPath_, overwrite: true);
timur
  • 14,239
  • 2
  • 11
  • 32
2

The .NET framework provides a built-in method to copy a file:

File.Copy(fileNameWithPath_, newfileNameWithPath_)

(Here, File is System.IO.File.)

Or alternatively:

using (FileStream inStream = new FileStream(fileNameWithPath_, FileMode.Open, FileAccess.Read))
using (FileStream outStream = new FileStream(newfileNameWithPath_, FileMode.Create, FileAccess.Write))
{
    inStream.CopyTo(outStream);
}
drf
  • 8,461
  • 32
  • 50