文件操作 C++, 共享方式打开

#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;

int main(int argc, const char* argv[])

{  


  fstream hFile;
  hFile.open("me.txt",ios::app, _SH_DENYNO); 
 hFile<< "adfdfdfdf\n";
 hFile<< "bdfdfdfdf\n";
 hFile<< "cdfdfdfdf\n";
 hFile<< "ddfdfdfdf\n";
 hFile.close();
    hFile.open("me.txt", ios::out | ios::in, _SH_DENYNO); //only deny write
    string str;
int count=0;
    while(std::getline(hFile, str))
{ count++;
    cout << str << endl;
}
cout <<count;
    hFile.close();
    cin.get();

    return 0;

}


共享方式参数可以是下面的值:
0x10 //_SH_DENYRW Denies read and write access to the file 
0x20 //_SH_DENYWR Denies write access to the file 
0x30 //_SH_DENYRD Denies read access to the file. 
0x40 //_SH_DENYNO Permits read and write access 


mode可以设置的方式如下:
ios::app 以追加的方式打开
ios::ate 文件打开后定位到文件尾
ios::binary 以二进制方式打开文件,默认是以文本方式打开
ios::in 文件以读(输入)方式打开
ios::out 文件以写(输出)方式打开
ios::trunc 如果文件存在,则把文件清空。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值