C++ 讀寫文件安全又簡潔的簡單實例。本站提示廣大學習愛好者:(C++ 讀寫文件安全又簡潔的簡單實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C++ 讀寫文件安全又簡潔的簡單實例正文
投稿:lqh
這篇文章主要介紹了C++ 讀寫文件安全又簡潔的簡單實例的相關資料,需要的朋友可以參考下C++ 讀寫文件安全又簡潔的簡單實例
實例代碼:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int get_file_content(string sFileName, string& sFileContent);
int main(int argc, char* argv[])
{
string sFileContent;
get_file_content("./test", sFileContent);
cout << sFileContent << endl;
return 0;
}
int get_file_content(string sFileName, string& sFileContent)
{
ifstream ifs (sFileName.c_str(), ifstream::in);
sFileContent.clear();
char c;
while (ifs.get(c)){
sFileContent.append(1, c);
}
ifs.close();
return 0;
}
int set_file_content(string sFileName, string& sFileContent)
{
ofstream ofs(sFileName.c_str(), ofstream::binary);
size_t nCount = sFileContent.size();
ofs.write (sFileContent.c_str(), nCount);
ofs.close();
return nCount;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!