C++ 讀寫文件安全又簡潔的簡單實例

成都創(chuàng)新互聯(lián)基于成都重慶香港及美國等地區(qū)分布式IDC機房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報價,主機托管價格性價比高,為金融證券行業(yè)成都服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨享,G口帶寬及機柜租用的專業(yè)成都idc公司。
實例代碼:
#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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
網(wǎng)站名稱:C++讀寫文件安全又簡潔的簡單實例
本文地址:http://www.yijiale78.com/article18/gjhegp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、用戶體驗、全網(wǎng)營銷推廣、軟件開發(fā)、企業(yè)建站、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)