Summary of fstream, ifstream, and ofsream usage

Source: Internet
Author: User
When writing a program, a piece of code uses fstream to create and read and write hidden files, but some strange problems are discovered: The hidden files are not created at the time of creation, but cannot be read or modified when they exist, the following three types of objects are tested and summarized: 1. Create a file when the test file does not exist.
========================================================== ====================== Fstream FS ("aaa.txt ", ios_base: In) not created
Fstream FS ("aaa.txt", ios_base: Out) Creation
Fstream FS ("aaa.txt", ios_base: trunc) is not created
Fstream FS ("aaa.txt"); // equivalent to downlink
Fstream FS ("aaa.txt", ios_base: In | ios_base: Out) not created
Fstream FS ("aaa.txt", ios_base: In | ios_base: trunc); not created
Fstream FS ("aaa.txt", ios_base: Out | ios_base: trunc); Create
Fstream FS ("aaa.txt", ios_base: In | ios_base: Out | ios_base: trunc); create the above summary:
Fstream is created when the file does not exist
Or use ios_base: out separately
Either ios_base: Out | ios_base: trunc
========================================================== ======================== Ofstream and fstream are not very different,
When you open it, no matter whether or not ios_base: Out is added, it will always be or once ("_ mode | ios_base: Out"), which can be inferred from the previous conclusion, files will be created in the following three cases, and none of them will be created: ofstream ofs ("aaa.txt ")
Ofstream ofs ("aaa.txt", ios_base: trunc)
Ofstream ofs ("aaa.txt", ios_base: In | ios_base: trunc) 2. Test the function of reading hidden files. ========================================================== ====================== Fstream FS ("aaa.txt ", ios_base: In) can
Fstream FS ("aaa.txt", ios_base: Out) cannot
Fstream FS ("aaa.txt", ios_base: trunc) cannot
Fstream FS ("aaa.txt"); // equivalent to downlink
Fstream FS ("aaa.txt", ios_base: In | ios_base: Out) can
Fstream FS ("aaa.txt", ios_base: In | ios_base: trunc); No
Fstream FS ("aaa.txt", ios_base: Out | ios_base: trunc); No
Fstream FS ("aaa.txt", ios_base: In | ios_base: Out | ios_base: trunc); cannot be summarized as follows:
Fstream open hidden files
Or use ios_base: In separately
Or ios_base: trunc is not allowed.
========================================================== ========================
Ifstream and fstream are not much different,
When you open it, no matter whether or not ios_base: In is added, it will always be or once ("_ mode | ios_base: In"), which can be inferred from the previous conclusion, you can open files in either of the following two cases:
Ifstream ifs ("aaa.txt ")
Ifstream ofs ("aaa.txt", ios_base: Out) 3. Read common files========================================================== ====================== Fstream FS ("aaa.txt ", ios_base: In) can
Fstream FS ("aaa.txt", ios_base: Out) can
Fstream FS ("aaa.txt", ios_base: trunc) cannot
Fstream FS ("aaa.txt"); // equivalent to downlink
Fstream FS ("aaa.txt", ios_base: In | ios_base: Out) can
Fstream FS ("aaa.txt", ios_base: In | ios_base: trunc); No
Fstream FS ("aaa.txt", ios_base: Out | ios_base: trunc); Can
Fstream FS ("aaa.txt", ios_base: In | ios_base: Out | ios_base: trunc); can be summarized as follows:
Fstream open common files
Ios_base: trunc must contain both ios_base: Out
========================================================== ========================
Ifstream and fstream are not much different,
When you open it, no matter whether or not ios_base: In is added, it will always be or once ("_ mode | ios_base: In"), which can be inferred from the previous conclusion, in one of the following cases, files cannot be opened, and other files can be:
Ifstream ofs ("aaa.txt", ios_base: trunc)

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.