ofstream和ifstream详细用法
1.插入器(<<)
2. 析取器(>>)
在fstream类中,有一个成员函数open(),就是用来打开文件的,其原型是:
参数:
mode: 要打开文件的方式
access: 打开文件的属性
打开文件的方式在类ios(是所有流式I/O类的基类)中定义,常用的值如下:
ios::ate: 文件打开后定位到文件尾,ios:app就包含有此属性
ios::binary: 以二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文
ios::in: 文件以输入方式打开(文件数据输入到内存)
ios::out: 文件以输出方式打开(内存数据输出到文件)
ios::nocreate: 不建立文件,所以文件不存在时打开失败
ios::noreplace:不覆盖文件,所以打开文件时如果文件存在失败
ios::trunc: 如果文件存在,把文件长度设为0
可以用“或”把以上属性连接起来,如ios::out|ios::binary
1:只读文件
2:隐含文件
4:系统文件
可以用“或”或者“+”把以上属性连接起来,如3或1|2就是以只读和隐含属性打开文件。
file1.open( "c:\\config.sys",ios::binary|ios:: in,0);
file1.open("c:\\config.sys"); <=> file1.open("c:\\config.sys",ios::in|ios::out,0);
ofstream file3( "c:\\x.123"); //以输出方式打开文件
打开的文件使用完成后一定要关闭,fstream提供了成员函数close()来完成此操作,如:file1.close();就把file1相连的文件关闭。
读写文件分为文本文件和二进制文件的读取,对于文本文件的读取比较简单,用插入器和析取器就可以了;而对于二进制的读取就要复杂些,下要就详细的介绍这两种方式
文本文件的读写很简单:用插入器(<<)向文件输出;用析取器(>>)从文件输入。假设file1是以输入方式打开,file2以输出打开。示例如下:
int i;
file1>>i; //从文件输入一个整数值。
dec 格式化为十进制数值数据 输入和输出
endl 输出一个换行符并刷新此流 输出
ends 输出一个空字符 输出
hex 格式化为十六进制数值数据 输入和输出
oct 格式化为八进制数值数据 输入和输出
setpxecision(int p) 设置浮点数的精度位数 输出
①put()
put()函数向流写入一个字符,其原型是ofstream &put(char ch),使用也比较简单,如file1.put('c');就是向流写一个字符'c'。
get()函数比较灵活,有3种常用的重载形式:
要读写二进制数据块,使用成员函数read()和write()成员函数,它们原型如下:
write( const unsigned char *buf, int num);
int n[5];
ifstream in( "xxx.xxx");
ofstream out( "yyy.yyy");
out.write(str1,strlen(str1)); //把字符串str1全部写到yyy.yyy中
in.read((unsigned char*)n, sizeof(n)); //从xxx.xxx中读取指定个整数,注意类型转换
in.close(); out.close();
成员函数eof()用来检测是否到达文件尾,如果到达文件尾返回非0值,否则返回0。原型是int eof();
和C的文件操作方式不同的是,C++ I/O系统 管理两个与一个文件相联系的指针。一个是读指针,它说明输入操作在文件中的位置;另一个是写指针,它下次写操作的位置。每次执行输入或输出时,相应的指针自动变化。所以,C++的文件定位分为读位置和写位置的定位,对应的成员函数是seekg()和seekp()。seekg()是设置读位置, seekp是设置写位置。它们最通用的形式如下:
ostream &seekp(streamoff offset,seek_dir origin);
ios::cur: 文件当前位置
ios::end: 文件结尾
file2.seekp(1234,ios::beg); //把文件的写指针从文件开头向后移1234个字节
这是MSDN中ofstream和ifstream的解释:
ofstream::ofstream
ofstream();
ofstream( const char* szName, int nMode = ios::out, int nProt = filebuf::openprot );
ofstream( filedesc fd );
ofstream( filedesc fd, char* pch, intnLength);
Parameters
szName
The name of the file to be opened during construction.
nMode
An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR (| ) operator. ThenMode parameter must have one of the following values:
- ios::app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with theostream::seekp function.
- ios::ate The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.
- ios::in If this mode is specified, then the original file (if it exists) will not be truncated.
- ios::out The file is opened for output (implied for all ofstream objects).
- ios::trunc If the file already exists, its contents are discarded. This mode is implied ifios::out is specified andios::ate, ios::app, andios:in are not specified.
- ios::nocreate If the file does not already exist, the function fails.
- ios::noreplace If the file already exists, the function fails.
- ios::binary Opens the file in binary mode (the default is text mode).
nProt
The file protection specification; defaults to the static integerfilebuf::openprot that is equivalent tofilebuf::sh_compat. The possiblenProt values are:
- filebuf::sh_compat Compatibility share mode.
- filebuf::sh_none Exclusive mode; no sharing.
- filebuf::sh_read Read sharing allowed.
- filebuf::sh_write Write sharing allowed.
To combine the filebuf::sh_read and filebuf::sh_write modes, use the logical OR (|| ) operator.
fd
A file descriptor as returned by a call to the run-time function_open or_sopen; filedesc is atypedef equivalent toint.
pch
Pointer to a previously allocated reserve area of length nLength. ANULL value (ornLength = 0) indicates that the stream will be unbuffered.
nLength
The length (in bytes) of the reserve area (0 = unbuffered).
Remarks
The four ofstream constructors are:
Constructor | Description |
ofstream() | Constructs an ofstream object without opening a file. |
ofstream( const char*, int, int ) | Contructs an ofstream object, opening the specified file. |
ofstream( filedesc ) | Constructs an ofstream object that is attached to an open file. |
ofstream( filedesc, char*, int ) | Constructs an ofstream object that is associated with afilebuf object. Thefilebuf object is attached to an open file and to a specified reserve area. |
All ofstream constructors construct a filebuf object. The first three use an internally allocated reserve area, but the fourth uses a user-allocated area. The user-allocated area is not automatically released during destruction.
ofstream::ofstream
ofstream();
ofstream( const char* szName, int nMode = ios::out, int nProt = filebuf::openprot );
ofstream( filedesc fd );
ofstream( filedesc fd, char* pch, intnLength);
Parameters
szName
The name of the file to be opened during construction.
nMode
An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR (| ) operator. ThenMode parameter must have one of the following values:
- ios::app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with theostream::seekp function.
- ios::ate The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.
- ios::in If this mode is specified, then the original file (if it exists) will not be truncated.
- ios::out The file is opened for output (implied for all ofstream objects).
- ios::trunc If the file already exists, its contents are discarded. This mode is implied ifios::out is specified andios::ate, ios::app, andios:in are not specified.
- ios::nocreate If the file does not already exist, the function fails.
- ios::noreplace If the file already exists, the function fails.
- ios::binary Opens the file in binary mode (the default is text mode).
nProt
The file protection specification; defaults to the static integerfilebuf::openprot that is equivalent tofilebuf::sh_compat. The possiblenProt values are:
- filebuf::sh_compat Compatibility share mode.
- filebuf::sh_none Exclusive mode; no sharing.
- filebuf::sh_read Read sharing allowed.
- filebuf::sh_write Write sharing allowed.
To combine the filebuf::sh_read and filebuf::sh_write modes, use the logical OR (|| ) operator.
fd
A file descriptor as returned by a call to the run-time function_open or_sopen; filedesc is atypedef equivalent toint.
pch
Pointer to a previously allocated reserve area of length nLength. ANULL value (ornLength = 0) indicates that the stream will be unbuffered.
nLength
The length (in bytes) of the reserve area (0 = unbuffered).
Remarks
The four ofstream constructors are:
Constructor | Description |
ofstream() | Constructs an ofstream object without opening a file. |
ofstream( const char*, int, int ) | Contructs an ofstream object, opening the specified file. |
ofstream( filedesc ) | Constructs an ofstream object that is attached to an open file. |
ofstream( filedesc, char*, int ) | Constructs an ofstream object that is associated with afilebuf object. Thefilebuf object is attached to an open file and to a specified reserve area. |
All ofstream constructors construct a filebuf object. The first three use an internally allocated reserve area, but the fourth uses a user-allocated area. The user-allocated area is not automatically released during destruction.
转载请注明本文地址: 超详细ofstream和ifstream详细用法