I have a binary file to which I want to append a chunk of data at the end of the file, how can I achieve this using C# and .net? Also is there anything to consider when writing to the end of a binary file? Thanks a lot for your help.
How to write code for C Program to Append / Add More Records in Binary File. fptr = fopen(“filename”, “wb”); Note that if we already use fopen(“filename”,”ab”); then there will be no change in the program. Since “ab” file open mode can create file for first time.
To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable. When opened using any text editor, the data is unrecognizable.
private static void AppendData(string filename, int intData, string stringData, byte[] lotsOfData) { using (var fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None)) using (var bw = new BinaryWriter(fileStream)) { bw.Write(intData); bw.Write(stringData); bw.Write(lotsOfData); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With