[C++]C++小笔记之Simple File read and write-- ofstream and ifstream
2014-01-02 14:16
525 查看
首先我们简单的复习一下cin.
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;
在cin 读取出错的情况下,因为被设了error flag, 所以在继续读取更多的input之前,需要使用 cin.clear()方法reset flag. clear()方法不仅会reset error flag; 它还可以reset EOF condition.
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:
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.
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.
bad() function returns false when something unexpected may go wrong.
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;
在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.
相关文章推荐
- 四种读写文件的方式:系统调用(open/read/write),C语言(fopen,fgets, fputs),C++(ifstream, ofstream getline,)泛型算法
- Something about file's read and write (C++)
- C++ Read a whole File using ifstream
- Configure file like format of ini and read data from ini file or write data to ini file
- Read and write excel file with format by pandas and xlwt libraries of python
- Sample of C++ read/write a text file
- Read and write a FileStream at the same time
- XCode can't open file (ifstream/ofstream) in C++ (XCode C++无法读写文件)
- 作品[RFileRead/WriteStream 练习] for s60 fp2
- Qt Write and Read XML File 读写XML文件
- C++ ofstream和ifstream用法
- C++文件读写详解(ofstream,ifstream,fstream)
- C++文件读写详解(ofstream,ifstream,fstream)
- C++文件读写详解(ofstream,ifstream,fstream)
- 学习笔记之ofstream 和ifstream的具体用法
- C++ 文件读写详解(七)(ofstream,ifstream,fstream)
- C++文件读写详解(ofstream,ifstream,fstream)
- How to mount partition with ntfs file system and read write access
- How to mount partition with ntfs file system and read write access
- c++文件处理ofstream,ifstream