源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-07-21 06:04 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C++ 读写文件安全又简洁的简单实例
[b]C++ 读写文件安全又简洁的简单实例[/b] [b]实例代码:[/b]
#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; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部