[C++]C++小笔记之Simple File read and write-- ofstream and ifstream

首先我们简单的复习一下cin.

int n;
cin >> n;

if user input a word instead of a number, four things will happen:

  1. the value of n remain unchanged;
  2. mismatch input is left in input queue;
  3. An error flag is set to the cin object;
  4. the call to the cin method, if converted to type bool, returns false.  if ( cin >> n) returns false;
在cin 读取出错的情况下,因为被设了error flag, 所以在继续读取更多的input之前,需要使用 cin.clear()方法reset flag.  clear()方法不仅会reset error flag; 它还可以reset EOF condition.


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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值