#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
int main(int argc, const char* argv[])
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();
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
int main(int argc, const char* argv[])
{
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 如果文件存在,则把文件清空。