de binary = _S_bin;
/// Open for input. Default for @c ifstream and fstream.
static const openmode in = _S_in;
/// Open for output. Default for @c ofstream and fstream.
static const openmode out = _S_out;
/// Open for input. Default for @c ofstream.
static const openmode trunc = _S_trunc;
具体含义如下:
ios_base::in 只读打开ios_base::out 只写打开ios_base::app 每次写之前移到文件尾ios_base::ate 打开文件后立刻定位到文件尾ios_base::trunc 打开文件并清空文件流ios_base::binary 以二进制模式进行读写操作
以ifstream方式打开文件,默认打开方式为in,以ofstream方式打开,默认打开方式为out | trunc,fstream是默认以in | out方式打开。如果要以ofstream打开文件,且同时保存文件的数据,只能通过显示的指定app打开模式。如下是fstream头文件中的open函数原型:
ifstream
void open(const char* __s, ios_base::openmode __mode = ios_base::in)
ofstream
void open(const char* __s, ios_base::openmode __mode = ios_base::out | ios_base::trunc)
fstream
void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out)
如果fstream打开方式是out,也会清空文件中数据。当文件以out方式打开(不包含in模式)时,如果文件不存在,会创建一个新文件。
输入输出操作符
istream,ostream类中都定义了用于输入输出的算法提取器(Arithmetic Extractor):'>>'和‘<<’操作符,可以提取内置数据类型的数据,例如,输入操作符>>用于从流缓冲区streambuf中提取数据,并输出到特定类型的变量中,例如istream头文件中具体定义如下:
//类内定义的,用于提取
__istream_type& operator>>(bool& __n);
__istream_type& operator>>(short& __n);
__istream_type& operator>>(unsigned short& __n);
__istream_type& operator>>(int& __n);
__istream_type& operator>>(unsigned int& __n);
__istream_type& operator>>(long& __n);
__istream_type& operator>>(unsigned long& __n);
__istream_type& operator>>(long long& __n);
__istream_type& operator>>(unsigned long long& __n);
__istream_type& operator>>(float& __f)
__istream_type& operator>>(double& __f)
__istream_type& operator>>(long double& __f)
__istream_type& operator>>(void*& __p)
//类外定义的,用于提取字符
template
inline basic_istream
& operator>>(basic_istream
& __in, unsigned char& __c); template
inline basic_istream
& operator>>(basic_istream
& __in, signed char& __c); template
inline basic_istream
& operator>>(basic_istream
& __in, unsigned char* __s); template
inline basic_istream
& operator>>(basic_istream
& __in, signed char* __s); //string类中对输入输出operator的重载,使其能支持string类型的输入输出 template
basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, ?????? basic_string<_CharT, _Traits, _Alloc>& __str); template
inline basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, ?????? const basic_string<_CharT, _Traits, _Alloc>& __str)
由于ifstream和ofstream派生于istream和ostream,所以fstream同样支持对文件流filebuf的输入输出操作。例如下例通过输入输出操作符读取文件中的数据:
#include
#include
#include
using namespace std; int main () { string fileName = "test.txt"; ofstream outOpt(fileName.c_str(), ios_base::out | ios_base::trunc); if(!outOpt) { cout<
>a[0]>>a[1]>>a[2]; //以空白符为分隔,从文件缓冲区中读取三个整数 inOpt>>b[0]>>b[1]; //以空白符为分隔,从文件缓冲区读取两个string数据(string类型对operator <<进行类重载,所以可以支持该操作) cout<
getline操作
在文件I/O的过程中,我们经常要处理的是按行对数据进行分析处理,istream类内定义了getline成员函数支持以特定的size或delimiter(默认为换行符)来提取定长或以特定分隔符分开的数据。同样,string头文件中也提供了getline全局函数,支持从istream类中,以特定的size或delimiter(默认为换行符)来提取数据。定义如下:
//istream类内定义,提供从输入流中读取一行数据到char*中
__istream_type&
getline(char_type* __s, streamsize __n, char_type __delim); //delim默认为换行
//string头文件定义(实际在basic_string.h中), 提供从输入流中读取一行数据到string中
template
basic_istream<_CharT, _Traits>&
getline(basic_istream<_CharT, _Traits>& __is,
basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); //delim默认为换行
使用如下:
#include
#include
#include
using namespace std; int main() { ifstream inFile("te