------------------------------------------------------------------------------
# Include <iostream>
# Include <fstream>
# Include <istream>
# Include <string>
Using namespace STD;
// Ifstream & open_file (ifstream
& In, const string & file ){
// Here all references are transmitted. Different from value transfer, reference transfer is fast and convenient, and value backup is not performed. On the other hand, it can be ensured
// Modify the parameters at the same time. For value passing, the parameter changes and does not change the real parameter. The reference is acceptable. References that do not want to be changed
// You can add a const to ensure that it is not changed.
//
In. Close ();
//
In. Clear ();
//
In. Open (file. c_str ());
// Return
In;
//}
Void main (){
Ifstream
Infile;
Infile. Open ("C: // readme.txt", IOS: In );
If (! Infile)
Cout <"can't open
The file "<Endl;
Fstream
OUTFILE;
OUTFILE. Open ("C: // 1.txt", IOS: Out | IOs: In); // The mode is a file attribute rather than a stream attribute.
String
Str_in;
While (Getline (infile, str_in ))
OUTFILE <str_in <Endl;
Infile. Close ();
OUTFILE. seekg (0, IOS: Beg); // This is very important. Judge the position of the file pointer at all times, or close it first, and then open it again.
//
OUTFILE. Close ();
//
OUTFILE. Open ("C: // 1.txt", IOS: Out | IOs: In); // The mode is a file attribute rather than a stream attribute.
//
While (Getline (OUTFILE, str_in ))
While (OUTFILE. Getline (str_in.begin (), 100, '/N '))
Cout <str_in.c_str () <Endl; // The printed content is empty. Why? It turns out that the file read/write pointer of the OUTFILE above has reached the end of the file, and the pointer needs to be located again in the file header
OUTFILE. Close ();
}
// Conclusion: for historical reasons, the I/O standard library still uses a C-language string instead of a string in C ++, so Str. c_str () conversion is required.
// There are three most important types of I/O operations: iostream is the output of the device, CIN >>, cout <, fstream (ifstream, ofstream ), that is, file operations.
// Getline is the most important thing for ifstream. File Operations are similar to device operations. You can also use ifstream_obj >>, orstream_obj <.
// What I mentioned above is in C ++. For I/O in C, sprintf is more important. formatted output is opposite to atoi, // used to convert a number to a string. I just thought that since we have ITOA, we still need sprintf. Now I want to think about a number that is not an integer.