I/O Stream class library of C ++ from scratch (2): opening, closing, and stream status of file streams (fstream, ifstream, and ofstream)

Source: Internet
Author: User

1. file stream

Ofstream, derived from ostream, used to write files
Ifstream, derived from istream, used to read files
Fstream, derived from iostream, is used to read and write files.


Ii. Open a file

After the stream object is described, you can use the open () function to open the file. When a file is opened, a connection is established between the stream and the file.
Function prototype

Void open (const char * filename, int mode = IOs: Out, int prot = _ sh_denyno );

Parameters
Filename: file name, which can contain (absolute and relative) paths
Mode: file opening Mode
Prot: protection mode


(1) file opening Mode

Opening Method description

IOS: In open a file for reading (default value of ifstream Stream)

IOS: out open a file for writing (default value of ofstream Stream)
IOS: The app finds the end Of the file before writing.
IOS: ate: After opening the file, immediately locate the file at the end of the file
IOS: trunc discard the current file content
IOS: nocreate (no longer supported) if the file to be opened does not exist, you cannot call the open () function using this parameter.
IOS: noreplace (no longer supported) if the file to be opened already exists, an error will be returned when you try to open it using the open () function.
IOS: Binary open a file in binary format, which is a text file by default.


(2) Protection Mode

# DEFINE _ sh_denyrw 0x10/* deny read/write mode */reject file read/write
# DEFINE _ sh_denywr 0x20/* deny write mode */reject file writing
# DEFINE _ sh_denyrd 0x30/* deny read mode */reject object read permission
# DEFINE _ sh_denyno 0x40/* deny none mode */Read and Write Permission
# DEFINE _ sh_secure 0x80/* secure mode */shared read, exclusive write

NOTE: If process a is opened with _ sh_denyrw, process B cannot read or write files.


(3) effective combination of file opening modes



You can also add the ATE mode to all the preceding open mode combinations. Adding the ATE mode to these modes only changes the initial location when the file is opened.
Before writing, locate the file at the end of the file.


(4) Description of opening a file

1. The file can also be opened through the constructor, for example:

Ofstream fout({out.txt ", IOS: Out );

2. The file can be opened as an enumeration constant or a bitwise OR expression consisting of multiple enumeration constants.
3. When you use the open member function to open a file, the file is created if the file specified by the character pointer parameter does not exist. (Out)
4. When the open mode does not include the IOS: ate or IOS: app option, the file pointer is automatically moved to the start position of the file, that is, the byte address is 0.

5. In effect, the specified out mode of ofstream is equivalent to the specified out and trunc modes.
6. By default, fstream objects are opened in both in and out modes.
7. files are not cleared when they are opened in and out at the same time.
8. If only the out mode is used, but the in mode is not specified, the file clears the existing data.
9. If both the out and app are specified, the app will not be cleared.
10. If the trunc mode is specified when the file is opened, the file will be cleared no matter whether or not the in mode is specified at the same time.


Iii. Stream status



Corresponding to the status bits of the flag, the IOS class also provides the following member functions to detect or set the stream status:


Bool rdstate (); // indicates the current status of the returned stream.
Bool EOF (); // If the returned value is not 0, it indicates that the end of the file is reached.
Bool fail (); // If the returned value is not 0, the operation failed.
Bool bad (); // If the returned value is not 0, an error occurs.
Bool good (); // If the returned value is not 0, the stream operation is normal.
Bool clear (INT flag = 0); // set the stream status to flag


To improve program reliability, check whether I/O Stream operations are normal in the program. When an error occurs in a stream operation, you can solve the problem through exception handling.


Iv. File close


Each file stream class provides a member function close () to close the file ()
Function: After a file is opened, You need to disable it to disconnect the file stream from the corresponding physical file and ensure the content that is finally output to the File Buffer, whether it is full or not, it will be immediately written into the corresponding physical file

Function prototype: void close ();

After the file corresponding to the file stream is closed, you can use the file stream to call the open member function to open other files. You 'd better clear the file first.


C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Include <cassert>
# Include <iostream>
# Include <fstream>

Using namespace STD;

Int main (void)
{
/*************************************** ***************/
// If no file exists, a file is created.
// Ofstream fout;
// Fout. Open ("test.txt ");
Ofstream fout ("test.txt", IOS: Out | IOs: APP );

// Determine the stream status
// If (fout. is_open ())
//{
// Cout <"succ" <Endl;
//}
// Else
// Cout <"failed" <Endl;

// If (fout. Good ())
//{
// Cout <"succ" <Endl;
//}
// Else
// Cout <"failed" <Endl;

// If (fout)
//{
// Cout <"succ" <Endl;
//}
// Else
// Cout <"failed" <Endl;

// If (! Fout)
//{
// Cout <"failed" <Endl;
//}
// Else
// Cout <"succ" <Endl;

Assert (fout );
Fout. Close ();

/*************************************** *********************/
// If the file does not exist, no file will be created
Ifstream fin ("test2.txt ");

// Assert (FIN); // The object does not exist and the assert fails.

Fin. Close ();
/*************************************** *******************/
// Ofstream fout1 ("test3.txt", IOS: In | IOs: Out | IOs: ATE );
// Ofstream fout2 ("test3.txt", IOS: In | IOs: Out | IOs: ATE );

// Ofstream fout1 ("test3.txt", IOS: In | IOs: Out | IOs: APP );
// Ofstream fout2 ("test3.txt", IOS: In | IOs: Out | IOs: APP );

// The app and the ATE can coexist. The app prevails.
Ofstream fout1 ("test3.txt", IOS: In | IOs: Out | IOs: app | IOs: ATE );
Ofstream fout2 ("test3.txt", IOS: In | IOs: Out | IOs: app | IOs: ATE );

Assert (fout1 );
Assert (fout2 );

Fout1 <"X ";
Fout2 <"Y"; // y is output after X

Fout1.close ();
Fout2.close ();
/*************************************** *************************/

// The app and trunc cannot coexist, and the stream status is fail.
Ofstream fout3 ("test.txt", IOS: Out | IOs: app | IOs: trunc );
If (fout3.good ())
{
Cout <"good" <Endl;
}
If (fout3.bad ())
{
Cout <"bad" <Endl;
}
If (fout3.fail ())
{
Cout <"fail" <Endl;
}
If (fout3.eof ())
{
Cout <"EOF" <Endl;
}

Fout3.clear ();
Fout3.open ("test.txt"); // clear and then open again
If (fout3)
{
Cout <"Open succ" <Endl;
}
Else
Cout <"Open failed" <Endl;

Fout3.close ();

Return 0;
}

Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

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.