C++文件操作一 fstream::open

本文详细介绍了如何使用 C++ 中的 filebuf 类进行文件的打开操作,包括不同模式的参数设置及其含义,例如追加、定位到文件末尾、读取、写入等模式,并解释了文件保护设置的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Open a file
 
void open( const char *_Filename,
 ios_base::openmode _Mode = ios_base::in | ios_base::out,
 int _Prot = (int)ios_base::_Openprot);

 

void open( const char *_Filename,
 ios_base::openmode _Mode );


void open( const wchar_t *_Filename,
 ios_base::openmode _Mode = ios_base::in | ios_base::out,
 int _Prot = (int)ios_base::_Openprot);

 

void open( const wchar_t *_Filename, ios_base::openmode _Mode );
 
Parameters
_Filename
 The name of the file to open.

_Mode
 One of the enumerations in ios_base::openmode.

_Prot
 The default file opening protection.

_Mode

An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode 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 the ostream::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   The file is opened for input. The original file (if it exists) will not be truncated.

ios::out   The file is opened for output.

ios::trunc   If the file already exists, its contents are discarded. This mode is implied if ios::out is specified, and ios::ate, ios::app, and ios: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).
Note that there is no ios::in or ios::out default mode for fstream objects. You must specify both modes if your fstream object must both read and write files.

_Prot

The file protection specification; defaults to the static integer filebuf::openprot, which is equivalent to the operating system default, filebuf::sh_compat, under MS-DOS. The possible nProt values are as follows:

filebuf::sh_compat   Compatibility share mode (MS-DOS only).

filebuf::sh_none   Exclusive mode — no sharing.

filebuf::sh_read   Read sharing allowed.

filebuf::sh_write   Write sharing allowed.
The filebuf::sh_read and filebuf::sh_write modes can be combined with the logical OR ( || ) operator.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值