Whether you read and write, you have
Read: From the external file to handle the data to the program, read data from the outside, so the input stream is defined, that is, the input stream object: ifsteam infile, INFILE is the input stream. This object is stored in the data stream read from the file. Suppose there is a file with MyFile.txt, there are two lines of digital data, specific methods: int A, b; ifstream infile; infile.open ("myfile.txt"); // Note file path infile >> A> > b; // Two lines of data can be successively read into the variable in infile.close () if it is a big multi-line storage text file can be read: char buf [1024]; // temporarily save reading File content string message; ifstream infile; infile.open ("myfile.js"); if (Infile.is_open ()) // The file is successful, indicating that there was written {while (Infile.Good () &&! Infile .eof ()) {MEMSET (BUF, 0, 1024); Infile.getLine (BUF, 1204); Message = BUF; ... // This may do some operations for Message COUT << Message << Endl } Infile.close ();
Write: Write the data in the program to the file to write out the data, that is, the data leaving the program, thus defining the output flow object Outfile, outfile is the output flow object, this object is used to write to write Data among files. Specific practice: Outfile.open ("myfile.bat"); //myfile.bat is the file name IF (outfile.is_open ()) {OUTFILE.IS_Open ()) {OUTFILE << Message << endl; // message is a program Data processed outfile.close ();} else {cout << "You can't open the file!" << endl;}