Both read and write operations must contain the fstream header file.
Read: reads data from external files to programs for processing.
For a program, data is read from the outside, so the input stream is defined, that is, the input stream object is defined: ifsteam infile, infile is the input stream object.
This object stores the data stream to be read from the file. Assume that the file named myfile.txt contains two rows of numerical data. The specific method is as follows:
Int A, B;
Ifstream infile;
Infile. Open ("myfile.txt"); // note the file path.
Infile> A> B; // two rows of data can be read continuously into the variable.
Infile. Close ()
If it is a large multi-row text file, you can read it like this:
Char Buf [1024]; // temporarily Save the Read File Content
String message;
Ifstream infile;
Infile. Open ("myfile. js ");
If (infile. is_open () // if the file is successfully opened, it indicates that something has been written.
{
While (infile. Good ()&&! Infile. EOF ())
{
Memset (BUF, 0, 10, 24 );
Infile. Getline (BUF, 1204 );
Message = Buf;
... // Some operations may be performed on the message.
Cout <message <Endl;
}
Infile. Close ();
}
Write: Write the processed data in the program to the file.
For a program, the data is written out, that is, the data leaves the program. Therefore, the output stream object ofstream OUTFILE is defined, and the OUTFILE is the output stream object, which is used to store the data to be written to the file. Specific Practices:
Ofstream OUTFILE;
OUTFILE. Open ("myfile. Bat"); // myfile. bat is the data storage file name.
If (OUTFILE. is_open ())
{
OUTFILE <message <Endl; // message is the data processed in the program.
OUTFILE. Close ();
}
Else
{
Cout <"file cannot be opened! "<Endl;
}
Example of C ++ reading/writing operations on files
/*/Read a line of characters from the keyboard, put the letters in the disk file fa2.dat in sequence, and then read it from the disk file into the program,
Change the lowercase letters to uppercase letters and store them in the disk fa3.dat */
# I nclude <fstream>
# I nclude <iostream>
# I nclude <cmath>
Using namespace STD;
//////////// Function for reading characters from the keyboard
Void read_save (){
Char C [80];
Ofstream OUTFILE ("f1.dat"); // open the file with an outputer
If (! OUTFILE ){
Cerr <"Open error! "<Endl; // note that cerr is used
Exit (1 );
}
Cin. Getline (C, 80); // read a line of characters from the keyboard
For (INT I = 0; C [I]! = 0; I ++) // processes the characters one by one until '/0' is encountered.
If (C [I]> = 65 & C [I] <= 90 | C [I]> = 97 & C [I] <= 122) {// ensure that the entered character is a character
OUTFILE. Put (C [I]); // saves letters to Disk Files
Cout <C [I] <"";
}
Cout <Endl;
OUTFILE. Close ();
}
Void creat_data (){
Char ch;
Ifstream infile ("f1.dat", IOS: In); // open the file as input
If (! Infile ){
Cerr <"Open error! "<Endl;
Exit (1 );
}
Ofstream OUTFILE ("f3.dat"); // defines the output stream f3.dat File
If (! OUTFILE ){
Cerr <"Open error! "<Endl;
Exit (1 );
}
While (infile. Get (CH) {// when the character is successfully read
If (CH <= 122 & ch> = 97)
Ch = ch-32;
OUTFILE. Put (CH );
Cout <ch;
}
Cout <Endl;
Infile. Close ();
OUTFILE. Close ();
}
Int main (){
Read_save ();
Creat_data ();
System ("pause ");
Return 0;
}
Note: http://www.jgzsk.com/Article_Show.asp? ArticleID = 640