C ++ File Processing

Source: Internet
Author: User

1. Files and streams

Each file does not end with a file Terminator. It ends with a specific byte number in the data structure maintained and managed by the system. When the file is opened, an object is created to associate the object with a stream.
The four objects Cin, cout, cerr, and clog are automatically generated. The communication channel between the stream provider associated with these objects and a specific file or device.

 

Ii. File processing includes header files

<Iostream. h> ---------- <fstream. h>

 

3. Create an ordered Access File

The file is opened after an ifstream (read), ofstream (output), and fstream (Read/output) object is created.
For an ofstream object, the file can be opened in IOS: Out (output data to a file) or IOS: APP (add data to the end of the file without modifying the existing data in the file ), when an existing file is opened on iOS: Out, all data in the file is deleted. If the specified file does not exist, use the file name to create it.
For example, ofstream outclientfile ("clients. dat", IOS: Out );
Open the ofstream constructor of the file to establish communication with the file.

Overview of file opening methods:

IOS: The app writes all the output to the end of the file.
IOS: ate open the file for output, and move the end of the file (usually used to add data to the file ). Data can be written anywhere in the file
IOS: In open the file for Input
IOS: out open the file for output
IOS: trunc Delete existing file content (this is also the default operation for iOS: Out)
IOS: When opening a file in binary (non-text) format and inputting or outputting the exit function to terminate the program, if the parameter is 0, the program is terminated normally, any other value indicates that the program is terminated due to an error.

 

4. read data from sequential access files

For example:

Ifstream inclientfile ("clients. dat", IOS: In); <br/> while (inclientfile> account> Name> balance );

Both the ifstream class and the ostream class provide member functions so that the program can reposition the "File Location Pointer". These members are the seekg of the ifstream class and the seekp of the ostream class, each istream object has a get pointer, which indicates the same number of bytes of input in the file. Each ostream object has a put pointer, which indicates that there is one output with the same number of bytes.
For example, inclientfile. seekg (0); move the file position pointer to the beginning of the file. The seekg parameter is usually an integer of the long type. Seekg also has 2nd parameters. You can specify the search direction. For IOs: Beg (default), it is located at the beginning of the stream, IOS: cur is located at the current position of the stream, and IOs :: end is located relative to the end of the stream. The object position pointer is an integer that specifies the relative position (offset) of the object starting with the object ).
For example:

Fileobject. seekg (n); // point to the nth byte starting with the file. <Br/> fileobject. seekg (n, IOS: cur); <br/> fileobject. seekg (Y, IOS: End); <br/> fileobject. seekg (0, IOS: cur); <br/> location = fileobject. tellg (); // get the stream Length

 

5. Update sequential access files

Case:
Original record: 300 white 0.00
Changed to: 300 Worthington 0.00
Because the length of the new record is greater than the length of the original record, the characters After 'O' in 'ton 'will be reset.
This is the next sequence record in the file. The reason is that in the formatted input/output model using the stream insertion operator <and stream reading operator>, the width of the field is not fixed, therefore, the record width is not fixed.
For example, 7.14-117 2047 25482 is an int value, although their internal storage occupies the same number of bytes, however, when they are printed to the screen using formatted text or stored on a disk, the site uses different fields. Therefore, formatting the output/input model is usually not used to update existing records.

 

6. Random File Access

Generating ordered files and searching specific information from ordered access files sequential access files is not suitable for quick access to applications, that is, to find the information of specific records immediately. A quick application is implemented by randomly accessing files. Each record of such files can be directly accessed without searching.
Each record in a random access file has the same length. Therefore, the function of the record keyword can be used to calculate the position of each record relative to the start point of the file.

7. Create Random Access Files

The ostream member function write outputs a fixed number of bytes starting from the specified position in the memory to the specified stream. When a stream is associated with a file, the data is written to the position indicated by the put File Location pointer. The istream member function reads a fixed number of bytes from the region where the specified stream is input to the specified address in the memory. If a stream is associated with a file, the byte is input starting from the file address specified by the get file position pointer.
For example, a 4-byte integer number is no longer used as OUTFILE <number;
Print 1 or 11 bits (add 10 bits and 1 BITs respectively)
OUTFILE. Write (reinterpret_cast <const char *> (& number), sizeof (number ));
The Write function requires a data of the const char * type as the first parameter. Therefore, we use the reinterpret_cast <const char *> force type conversion operator to change the number address to the const char * pointer. The second parameter is an integer of the size_t type, which specifies the number of bytes written.
The Random Access File handler rarely writes only one domain to a file, which is usually written at a time, such as a struct or
Class Object.
For example:

Struct clientdata <br/>{< br/> int accountnumber; <br/> char lastname [15]; <br/> char firstname [10]; <br/> double balance; <br/>}; </P> <p> int main () <br/>{< br/> ofstream outcredit ("credit. dat ", IOS: Binary); <br/> If (! Outcredit) <br/>{< br/> cerr <"file cocould not be opened. "<Endl; <br/> exit (1); <br/>}< br/> clientdata blankclient = {0," "," ", 0.0 }; <br/> for (INT I = 0; I <100, I ++) <br/>{< br/> outcredit. wirte (<br/> reinterpret_cast <const char *> <br/> (& blankclient), sizeof (clientdata); <br/>}< br/> return 0; <br/>}

 

8. Random Data Writing to Random Access Files

The seekp and Write Functions of ostream are used to save data to the specified location of the file.
Outcredit. seekp (client. accountnumber-1) * sizeof (clientdata ));
Place the object's "put" File Location pointer in the byte location (client. accountnumber-1) * sizeof (clientdata.
The object outcredit of ofstream is opened by Using IOS: ate. The "put" File Location pointer is initially at the end of the file, but data can be written anywhere in the file.

 

9. Read data sequentially from random access files

Example:

Incredit. Read (reinterpret_cast <char *> (& client), sizeof (clientdata ))

Example:

Int main () <br/>{< br/> ifstream incredit ("credit. dat", ISO: In); <br/> If (! Increadit) <br/>{< br/> cerr <"file cannot open" <Endl; <br/> exit (1 ); <br/>}< br/> clientdata client; <br/> incredit. read (reinterpret_cast <char *> (& client), <br/> sizeof (clientdata); <br/> while (incredit &&! Incredit. EOF () // when a read error occurs or the end of the file exits <br/>{< br/> If (client. accuntnumber! = 0) outputline (cout, <br/> client); <br/> incredit. read (reinterpret_cast <char *> (& client), <br/> sizeof (clientdata); <br/>}< br/> return 0; <br/>}< br/> void outputline (ostream & output, const clientdata & C) <br/> {<br/> output <........................ <Endl; <br/>}

 

 

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.