. 1 #include <the iostream>
2 #include <the fstream> // header file reading and writing
. 3 #include < String >
. 4 the using namespace STD;
. 5 / *
. 6 . 1 text file write file
. 7 1 contains header
. 8 #include <the fstream >
. 9 2 creates a stream object
10 ofstream OFS;
. 11 . 3 and open manner specified path
12 is ofs.open (path open);
13 open:
14 iOS :: open reading documents in
15 iOS OUT write file opened ::
16 ios :: ate Open from the File Last
17 ios :: App additional ways to open
18 ios :: trunc If the file has been deleted in the first and saw
19 ios :: binary binary
20 4 Writing content
21 "to write data" ofs << < <endl;
22 is . 5 close the file
23 is ofs.close ();
24 * /
25 void Write () {
26 is // . 1 comprises a header file #include <the fstream>
27 // 2 creates a stream object
28 ofstream OFS;
29 // . 3 open and specify a path
30 ofs.open ( " named text.txt in ", IOS :: OUT );
31 is // . 4 to write the contents
32 OFS << " to write data " << endl;
33 is OFS << " to write data 2 " << endl;
34 is OFS << " Write Data Point 3 " << endl;
35
36 // . 5 close the file
37 [ ofs.close ();
38 is }
39
40 / *
41 is 2 to read document text file
42 is 1 contains the file header
43 is #include <the fstream>
44 2 Create a stream object
45 the ifstream IFS;
46 is . 3 and the specified path Open
47 ifs.open (path Open);
48 Open:
49 iOS :: open reading documents in
50 iOS :: OUT write open file
51 is iOS :: ATE from end of file open
52 ios :: App additional ways to open
53 ios :: trunc If the file has been deleted in the first and saw
54 ios :: binary binary
55 to read four ways 4
56 IFS << "to write data" << endl;
57 5 Close file
58 ifs.close ();
59 * /
60
61 is void Read () {
62 is // . 1 header
63 @ 2 to create a stream object
64 the ifstream IFS;
65 // . 3 determines whether the open succeeded to open the file
66 ifs.open ( " named text.txt in " , iOS :: in );
67 IF (! ifs.is_open ()) {
68 COUT << " file open failed! " << endl;
69 return ;
70 }
71 is // . 4 four ways read data
72 // first embodiment
73 is // char buf [1024] = {0};
74 //while (ifs >> buf) {
75 // cout << buf << endl;
76 //}
77
78 // 第二种
79 //char buf[1024];
80 //while (ifs.getline(buf, sizeof(buf))) {
81 // cout << buf << endl;
82 //}
83
84 // 第三种
85 //string buf;
86 //while (getline(ifs, buf)) {
87 // cout << buf << endl;
88 //}
89
90 //The fourth is not recommended
91 is char C;
92 the while (. (C = IFS GET ! ()) = The EOF) {
93 COUT << C;
94 }
95
96
97 // . 5 closes the flow
98 ifs.close ();
99 }
100
101 int main () {
102
103 Read ();
104
105 System ( " PAUSE " );
106 return 0 ;
107 }