Is written to a text file
Text I / O cout
It must include the header file iostream
Ostream iostream header file defines processing for outputting a class of
iostream declares a variable named cout of ostream (Object)
You must specify the namespace std;
And operators can be used cout << binding to display various types of data.
File I / O similar thereto
It must include the header file fstream
Ofstream fstream header file defines a class for processing output.
Ofstream need to declare one or more variables (objects), and the way you like to be named.
You must specify the namespace std;
We need to associate it with a file ofstream objects. open () method.
After finished with the file, the method should be used close () to close it.
Use ofstream objects and operator << outputs various types of data may be combined.
Although the header file iostream provides a predefined named cout is an ostream object, but must declare their ofstream objects, give it a name and associate it with the file.
// declare an object ofstream the outFile; ofstream FOUT; // object associated with a particular file outFile.open ( " fish.txt " ); char filename [ 50 ]; cin >> filename; fout.open (filename);
The method open () accepts a C-style string as a parameter, which may be a literal string, or a string stored in the array.
//此种对象的使用方法 double wt=125.8; outFile <<wt; char line[81]="Objects are closer than they appear."; fout<<line<<endl;
In short, after declaring a ofstream objects and their associated Tong files, you can use it to use as cou. Cout for all the operations and methods (e.g., <<, endl, setf ()) may be used ofstream objects.
Summarizes the main steps:
1. include the header file fstream.
2. Create a ofstream object.
3. The ofstream object is associated with a file up.
4. As with cout like to use the ofstream object.
#include <the iostream> #include <the fstream> // header file containing the fstream the using namespace STD; int main () { char Automobile [ 50 ]; // character arrays, for storing car brands and models int year; // production Year Double a_price; // cost price Double d_price; // discount ofstream the outFile; // define a file output stream object the outFile // after you define a file stream object, you can use the member functions need to open the file open operation //Open void (const unsigned char * filename, the MODE int, int Access = filebuf: openprot); // filename is character pointer, specify the name of the file you want to open // the MODE specifies the file is opened (ostream default ios :: OUT | ios :: trunc) // Access the specified file system properties, and generally the default outFile.open ( " carinfo.txt " , ios :: OUT | ios :: trunc); cout << " enter the make and model of car: : " ; cin.getline (Automobile, 50 ); COUT << " enter the year: " ; CIN >> year; COUT << " input cost:" ; CIN >> a_price; d_price = 0.913 * a_price; cout << Fixed ; // output float usual way cout.precision ( 2 ); // set with an accuracy of 2, and return to the previous settings cout .setf (ios_base :: showpoint); // display float zero behind the decimal point. // output on screen cout << " the Make and Model: " << Automobile << endl; cout << " year: " << year << endl; cost<< "Was asking $" << a_price << endl; cout << "Now asking $" << d_price << endl; outFile << fixed; outFile.precision(2); outFile.setf(ios_base::showpoint); //输出到文件,类似于cout,将右边的输入到文件中 outFile<< "Make and model:" << automobile << endl; outFile << "year:" << year << endl; outFile<< "Was asking $" << a_price << endl; outFile<< "Now asking $" << d_price << endl; //使用完毕,clos关闭 outFile.close(); return 0; }