C++读写文件安全又简洁的简单实例-成都快上网建站

C++读写文件安全又简洁的简单实例

C++ 读写文件安全又简洁的简单实例

成都创新互联基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业成都服务器托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

实例代码:

#include  
#include  
#include  
 
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; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


当前名称:C++读写文件安全又简洁的简单实例
URL分享:http://kswjz.com/article/gjhegp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流