主要使用ofstream 和ifstream 的技术 纯c++实现
有时间感觉使用qt的文件操作没有c++的适用 特别是遇到中文或者\0 的特殊字符时 还是c++有效
void WriteFile(const string & sFilePath, const string & sContent)
{
ofstream out(sFilePath.c_str(), ios::binary);
if (out.is_open())
{
out.write(sContent.c_str(), sContent.size());
out.close();
}
return;
}
void ReadFile(const string & sFile, string & sContent)
{
ifstream in(sFile, ios::binary);
if (!in.is_open())
{
cout << "Error opening file"; exit(1);
}
in.seekg(0, ios::end);
int isize = in.tellg();
in.seekg(0, ios::beg);
unsigned char *pBuf = new unsigned char[isize];
in.read((char *)pBuf, isize);
copy(pBuf, pBuf + isize, back_inserter(sContent));
if (nullptr != pBuf)
{
delete [] pBuf;
pBuf = nullptr;
}
return;
}