Dear All,
Anybody know what is binary files? And How to write them in C#. I have been using streamreader and streamwriter to work with a text file but I have no idea of the binary file.
Kindest Regards,
E
blah blah blah is here! blah blah » Close
1 answers
Binary files are simply non-text files i.e. files which contain raw bytes written in a certain format or encoding.
Examples include exes, dlls, image and audio files.
You can read from and write to binary files using the BinaryReader and BinaryWriter classes.
A quick way to read or write the whole file is to use the File.ReadAllBytes() or File.WriteAllBytes() methods.
answered one year ago by:
17279
301
Is it different than text file? And It is binary, so I can only write '0's and '1's? Or can I still write a string or an int? Some website also mention binary as executable files...wew...I am confused now
301
Yeah I have done using the Binary Reader and Binary Writer.. Thank you vulpes :) But I make the extension *.dat..is it okay..? Thanks again :)
17279
Yes, binary files are any type of file other than plain text files, including executables. As such they can contain any sequence of bytes (a byte is, of course, ultimately a sequence of eight '0's or '1's). You can write strings to such files but there are several ways to encode them. For example, strings can be encoded as ASCII, Unicode, UTF-8 etc. They can also be prefixed with their length or terminated with a null byte (a byte with a value of zero). Ints are usually encoded as a sequence of 4 bytes with the least signifcant byte first (the so called little-endian order). In order to understand a particular binary format, you need to know what encodings are used so you can use the appropriate BinaryReader/BinaryWriter methods to read from or write to the file. You can use any extension you like as long as it doesn't result in confusion with existing standard extensions. So, yes, *.dat is fine as it just indicates some sort of data file.