C++ 讀寫文件安全又簡潔的簡單實(shí)例
更新時間:2017年06月15日 14:56:43 投稿:lqh
這篇文章主要介紹了C++ 讀寫文件安全又簡潔的簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
C++ 讀寫文件安全又簡潔的簡單實(shí)例
實(shí)例代碼:
#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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++算術(shù)運(yùn)算符與類型轉(zhuǎn)換
這篇文章主要介紹了C++算術(shù)運(yùn)算符與類型轉(zhuǎn)換,C++當(dāng)中提供5種基礎(chǔ)的算術(shù)運(yùn)算符,分別是加法、減法、乘法、除法和取模。下main我們就一起來看看下面文章得具體舉例與說明,需要的朋友可以參考一下,希望對你有所幫助2021-11-11
淺析C++調(diào)用Java的Jar包(帶參數(shù))問題
這篇文章主要介紹了C++調(diào)用Java的Jar包(帶參數(shù))問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
教你如何使用qt quick-PathView實(shí)現(xiàn)好看的home界面
pathView的使用類似與ListView,都需要模型(model)和代理(delegate),只不過pathView多了一個路徑(path)屬性,顧名思義路徑就是item滑動的路徑,下面給大家分享qt quick-PathView實(shí)現(xiàn)好看的home界面,一起看看吧2021-06-06
詳解C語言求兩個數(shù)的最大公約數(shù)及最小公倍數(shù)的方法
這篇文章主要介紹了C語言求兩個數(shù)的最大公約數(shù)及最小公倍數(shù)的方法,輾轉(zhuǎn)相除法和輾轉(zhuǎn)相減法在解決這種問題時最常用到,需要的朋友可以參考下2016-03-03
C++深入講解new與deleted關(guān)鍵字的使用
這篇文章主要介紹了C++中new與deleted關(guān)鍵字的使用,new在動態(tài)內(nèi)存中為對象分配空間并返回一個指向該對象的指針;delete接受一個動態(tài)對象的指針, 銷毀該對象, 并釋放與之關(guān)聯(lián)的內(nèi)存2022-05-05

