Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append data to a binary file?

Tags:

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.

like image 623
ryudice Avatar asked Mar 07 '10 23:03

ryudice


People also ask

Will open a binary file in append mode in C?

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.

How does Python handle binary files?

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.


1 Answers

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);     } } 
like image 153
Jesse C. Slicer Avatar answered Oct 04 '22 15:10

Jesse C. Slicer