===================================================================
When writing a program, it is often necessary to manipulate files, such as reading data from a file, saving some data to a file through a program, and so on.
Here are some simple actions for C + + for files.
===================================================================
I. File output
The following 5 steps are generally required to output data from a program to a file:
① contains fstream header files: #include <fstream>
② set up Ofstream object: Ofstream ocout;
③ associating an object with a file: Ocout.open ("file.txt");
④ uses this object to output data to the file test:ocout<< "C + + file operation. ”;
⑤ Close the connection to the file: Ocout.close ();
Some notes:
1. Use the above steps to output the data to the File.txt file instead of the screen.
2. Although the final step is not ocout.close (), in some cases data can be exported to the file, but for security reasons, Ocout.close () must write.
3.ocout.open ("file.txt"), this file is created automatically if there is no file.txt file.
Complete program:
After the program runs, open the file, you can see the text entered:
Two. File read
The method of opening the file to read the data and the method of outputting the data to the file are basically the same, it also requires 5 steps:
① contains fstream header files: #include <fstream>
② set up Ifstream object: Ifstream icin;
③ associating an object with a file: Icin.open ("file.txt");
④ uses the object to read the data in the file test into the array temp:icin>>temp;
⑤ Close the connection to the file: Icin.close ();
Examples of programs:
Run it:
You can see that a word in the file has been read into.
If you add a space to the middle of this sentence in the file: C + + file operation
After running:
You can see that only the data in front of the space is read,
In fact, when reading the data, the,>> encounters a space or ' \ ' will stop reading, and if you want to continue reading the subsequent data, use >> to continue reading.
Read a space and what follows it
Use Getline () to do
********************************************************************************
1 Basic_istream::getline2 3Basic_istream<elem, tr>&Getline (4 5Char_type *_str,6 7 streamsize _count8 );9 TenBasic_istream<elem, tr>&Getline ( One AChar_type *_str, - - streamsize _count, the - Char_type _delim -);
Each of the parameters is as follows:
_count
The number of characters to read from Strbuf.
Number of characters read
_delim
The character that should terminate the read if it is encountered before _count.
End symbol (The default end symbol is ' \ n '), where you can replace the default ending symbol with a custom ending symbol, which means that the _delim is stopped within the range of read characters.
_str
A string in which to write.
Read to a string in a stream
****************************************************************************************************
Modify the above code:
After running
There are spaces in between, and you can output the
Simple operations for C + + files