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++读写文件安全又简洁的简单实例
文章地址:http://kswsj.cn/article/gjhegp.html

其他资讯