首先我们简单的复习一下cin.
int n;
cin >> n;
if user input a word instead of a number, four things will happen:
- the value of n remain unchanged;
- mismatch input is left in input queue;
- An error flag is set to the cin object;
- the call to the cin method, if converted to type bool, returns false. if ( cin >> n) returns false;
ofstream -- Simple File Write To a txt File.
与cout用法相同,当要写内容进入文件时, ofstream objects可以使用所有cout同名的函数。
- must include a fstream header file;
- fstream header file defines an ofstream class for handling output.
- name one or more ofstream objects;
- using namespace std OR use std:: prefix;
- associate ofstream object with a specific file. One way to do so is to use open("test.txt") method;
- use close() method to close the file when finished write the file.
- use ofstream object with << operator to outpu a variety of data types;
Create a file:
fout.open("test.txt"); // if no such file, it will be created; if have file, clear its contents.
OR
char filename[50];
cin >> filename; //enter the filename that wants to write
fout.open(filename);
double wt=125.8;
fout<<wt; //write a number to txt file
char line[4] = "xyz";
fout<< line <<endl;
fout.close();
char automobile[50];
int year;
double a_price, d_price;
ofstream outFile;
outFile.open("carinfo.txt");
cout << "Enter the make and model of the car: ";
cin.getline(automobile, 50);
cout << "Enter the model year: ";
cin >> year;
cout << "Enter the original asking price: ";
cin >> a_price;
d_price = 0.913 * a_price;
cout << fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << "make and model: " << automobile << endl;
cout << "year: " << year << endl;
cout << "price: " << d_price << endl;
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "make and model: " << automobile << endl;
outFile << "year: " << year << endl;
outFile << "price: " << d_price << endl;
outFile.close();
ifstream -- Simple File Read From a txt file.
与cin用法相同,当要写内容进入文件时, ifstream objects可以使用所有cin同名的函数。
- must include a fstream header file;
- fstream header file defines an ifstream class for handling input;
- name one or more ifstream objects;
- if stream use >> operator to read a variaty of data types;
- ifstream object use get() function to read a individual character and use getline() fucntion to read a line of characters.
- ifstream object use eof(), fail() functions to monitor for the success of an input attempt;
- ifstream object itself, when used as a test condition is converted to boolean value true if last read attemp scceded and to false if otherwise.
char filename[50];
ifstream readFile;
cout << "Enter the name of the data file: ";
cin.getline(filename, 50);
readFile.open(filename);
if (!readFile.is_open()) {
cout << "Could not open file " << filename << endl;
cout << "Program terminating.\n";
return false;
}
char content[100];
readFile >> content;
while (readFile.good()) {
cout << content;
readFile>>content;
}
if (readFile.eof()) {
cout << " Reached at the end of file." << endl;
} else if (readFile.fail()) {
cout<<"Input terminated by data mismatch" <<endl;
} else {
cout<<"Input terminated for unknown reason"<<endl;
}
is_open("test.txt") function shows if a file can be opened succesfully.
fail() function returns true if read EOF or a type mis-match. e.g.
readFile >> content;
bad() function returns false when something unexpected may go wrong.