C # access files in binary mode

Source: Internet
Author: User

Yesterday we discussed how to serialize memory objects and store and read them as binary files. However, in actual programming applications, not all objects need to be written to a file. What if we only want to write part of the data (such as a string or a number? In this case, you only need to use the binarywriter and binaryreader classes in the system. Io namespace.

After studying the Code read from some C ++/C binary files written by my predecessors, I found that C # is more intelligent in binary file read/write. C/C ++ precisely controls the location of the memory to read the corresponding bit. in C #, you do not need to consider this, but you only need to know the order of the data you store, read data in sequence. For example, if you use the void of the binarywriter object
The write (int I) function stores an int32 bit of data, and then stores a string with void write (string Str) and void write (double
D) a double-precision floating point number is saved. During reading, you only need to use the int of the binaryreader object in sequence.
Readint32 (), string readstring (), double
Readdouble () reads data streams sequentially to obtain values of the int, sting, and double types. You do not need to care about which data stream reads from which data stream ends. You should leave it to C # To determine it. Of course, inconsistent access sequence may cause unexpected values to be read or nothing to be read out, or out of the data stream range.

In addition, if you want to control the read/write location by yourself like C ++/C, you can also use
Seek (INT offset, seekorigin
Origin) function.

The following sample code (only key code is provided ):

String Path = application. startuppath + "\ data. DB "; // binary file path private void button#click (Object sender, eventargs e) // save as file {person per = new person () {name =" 文llele ", age = 12, Height = 1.50}; // instantiate a data stream ST = new filestream (path, filemode. create); binarywriter BW = new binarywriter (ST); BW. write (Per. age); // int BW first. write (Per. name); // string BW. write (Per. height); // The last double BW. close (); ST. close ();} private void button2_click (Object sender, eventargs e) // read the file {stream ST = new filestream (path, filemode. open); binaryreader BR = new binaryreader (ST); person Pr = new person (); // an unspecified PR. age = BR. readint32 (); // first int PR. name = BR. readstring (); // then string PR. height = BR. readdouble (); // The last double textbox1.text = Pr. tostring (); // The final text box will display: Name: Wenwen Lele \ r \ n age: 12 \ r \ n height 1.50 BR. close (); ST. close () ;}} class person {public string name {set; get;} public int age {set; get;} public double height {set; get ;} public override string tostring () {return "name:" + name. tostring () + "\ r \ n age:" + age. tostring () + "\ r \ n Height:" + height. tostring ();}}

If you want to use the long seek (INT offset, seekorigin origin) function of stream to set where to start reading, the read code should be changed to the following:

Private void button2_click (Object sender, eventargs E) // read the file {stream ST = new filestream (path, filemode. open); binaryreader BR = new binaryreader (ST); person Pr = new person (); // an unspecified example St. seek (4, seekorigin. begin); // because the first binary file to store is a 32-bit age (32-bit integer by default), which occupies 4 bytes, so the name starts from 4th bytes (8 bits are 1 byte, 32 bits are 4 bytes) PR. name = BR. readstring (); ST. seek (0, seekorigin. begin); // age record at the start of PR. age = BR. readint32 (); ST. seek (-8, seekorigin. end); // The height is 64-bit dual-precision floating point type, which occupies 8 bytes and is placed at the end. Therefore, it counts 8 bytes of PR from the back to the front. height = BR. readdouble (); textbox1.text = Pr. tostring (); // The final text box will display: Name: 文llele \ r \ n age: 12 \ r \ n height 1.50 and the first method does not change BR. close (); ST. close ();}

The two reading methods can have the same effect. To fix the length of the string type, you can also use stringbuilder to create a specified length string.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.