Study Notes of iostream library in C ++ standard library (2) Use of fstream library and ofstream class

Source: Internet
Author: User

The iostream library not only supports input and output from terminal devices, but also file input and output. File-related input and output class declarations are included in the fstream header file. There are three classes responsible for file input and output.

1) ifstream class: Derived from istream class.
2) ofstream class: Derived from ostream class.
3) fstream class: Derived from iostream class.

Because the input and output of files are different from the input and output of the keyboard and mouse, generally there is only one keyboard device on the PC. Therefore, the iostream library declares an istream object cin, this object is responsible for retrieving data from the keyboard, and many file devices are used in the system, therefore, the iostream library cannot create an ifstream object for each file on the machine and an ofstream object for Data Writing, therefore, when reading or writing data to a file, you must create an ifstream or ostream object.

The default constructor of the ofstream class is as follows:

Ofstream: ofstream ( Const   Char * Filename, Int Mode = IOS :: Out , Int Openport = Filebuf: openport );

Filename is the name of the file to be opened,
Mode is enabled,
Openport is the attribute of opening a file.

The mode can be set as follows:
IOS: The app is opened in append mode.
IOS: ate file is opened and located at the end of the file
IOS: Binary open a file in binary format. It is opened in text format by default.
IOS: In file opened in read (input) Mode
IOS: Out file opened in write (output) Mode
IOS: trunc clear the file if the file exists.
The preceding attributes are connected by "|" (by bit or.

The openprot attributes are as follows:
0 common files
1 read-only file
2. Implicit File
4. system files
The preceding attributes can be organized by adding or by bit. For example, 1 | 2 and 3 indicate both read-only and implicit files.

In Windows, do not use the third parameter. If the third parameter is added, the third parameter is the file sharing mode, that is, when the file is opened, whether other processes can read and write the file.
The sharing mode parameter can be the following values:
0x10 // _ sh_denyrw denies read and write access to the file
0x20 // _ sh_denywr denies write access to the file
0x30 // _ sh_denyrd denies read access to the file.
0x40 // _ sh_denyno permits read and write access
The error "invalid sharing flag" is reported for other values.

Ofstream hfile ( " C: \ 1.txt " , IOS :: Out , _ Sh_denyrw ); // _ Sh_denyrw is deny read and write

If ( ! Hfile) // If the file cocould open, hfile is a handle, else is zero
{
Cout <   " Write fail! "   < Endl;
Cout <   " Access is denies, maybe the file is readonlys, or use deny read opened of other process. "   < Endl;
}
Else
{< br> hfile " by coderlee writes " ;< br> cout " write success! " Endl;
}
Hfile. Close (); // Opened file need close.

The above is an example of file writing.CodeFirst, open the file, and then determine whether it is 0. If it is 0, the system will prompt write fail; otherwise, the system will prompt write success.

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.