c# - Writing to .bin binary file -


i trying write integer numbers binary file, keeps giving weird characters in binary file. example, try write 2000, in file strange. how fix it? couldn't find solution anywhere.

i use following code:

 //create file         filestream fs = new filestream("iram.bin", filemode.create);         // create writer data.         binarywriter w = new binarywriter(fs);  w.write((int) 2000);  w.close(); fs.close(); 

i think problem not reading data properly.

you need read data using binaryreader so...

    using (filestream fs2 = new filestream("iram.bin", filemode.open))     {         using(binaryreader r = new binaryreader(fs2))         {             var integervalue = r.readint32();         }     } 

unless of course want write text file in case don't want binarywriter write data out.

if want write out text data this... (be sure set encoding need)

    using (var tw = new streamwriter("iram.txt", true, encoding.ascii))     {         tw.writeline(2000);     } 

edit: jesse mentioned want wrap disposable objects in using blocks.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -