Recently, I started to read c ++. I used to start learning C ++ in my freshman winter vacation (I took a C ++ class in my freshman year and liked it very much ), as a result, my sister misled me by saying "C ++ is out of date, learning Java". As a result, my university has been working since the next semester of my freshman year, it is mainly used in Java. I am ashamed of myself, but now I have decided to write a blog while learning.
1. Write c ++ text
It mainly uses the ofstream class for processing output, which is included in the fstream header file.
# Include <iostream> # include <fstream> using namespace STD; void writefile () {ofstream OUTFILE; OUTFILE. open ("test.txt"); cout <"Please input your name:"; char input [10]; CIN> input; int age; cout <"Please input your age:"; CIN> age; double money; cout. SETF (IOs: fixed); cout. precision (6); cout <"Please inpu your money:"; CIN> money; double Other = money * 0.925; cout <"money: "<other <Endl; OUTFILE <input <Endl; // write name OUTFILE <age <Endl; // write age OUTFILE <other <Endl; // write money OUTFILE. close () ;}int main () {writefile (); Return 0 ;}
The above is a simple piece of code for writing files.
First, create an output class ofstream OUTFILE;
Open a file OUTFILE. Open ("test.txt ");
Then there is some simple input information.
Cout. SETF (IOs: fixed) indicates that it is set to the fixed-point output format.
Cout. Precision (6) means to set the precision to 6 bits.
Here we will mainly learn the floating point output with the specified precision.
In addition, ofstream can be used the same as cout <. However, ofstream is the output data in the file, while cout is the output data to the console.
Close with OUTFILE. Close. If you forget to close it, it will be automatically closed after the program is terminated.
2. c ++ text reading
It mainly uses the ifstream class for processing output, which is included in the fstream header file.
#include<iostream>#include<fstream>using namespace std;void readFile(){ifstream inFile;inFile.open("test.txt");if(inFile.is_open()){char input[10];inFile >> input;int age;inFile >> age;double money;inFile >> money;cout << "ReadFile Complete" << endl;cout << "Name:" << input << endl;cout << "Age:" << age << endl;cout << "Money:" << money << endl;}}int main(){readFile();return 0;}
Similarly, the ifstream class is used to read files.
However, when reading a file, the system finally checks whether the file is opened, because if the file is not opened successfully, an error will occur during reading.
Infile. is_open () is used for determination.
Like ofstream, ifstream can be used like Cin>. Only the former is used to read data from the file, and the latter is used to read the input from the console.
Of course, you can also read the entire row.
Example: Char line [81];
Infile. Getline (line, 80 );
If the object is read cyclically, You need to determine whether the object has reached the end of the object.
You can use if (infile. EOF () {} to determine whether the EOF is reached, and then use else if (infile. Fail () to check whether the EOF and type do not match. It is used to determine the reason why the read loop ends and perform other operations.
Merge the two parts of the Code:
PS: the simple file read and write in C ++ is here. I have only a short graduation and have limited experience. I hope you can point out mistakes and make progress together.
In addition, javafx scene builder is also used in the evening when I recently got home from work. I should write relevant blog posts recently.