c++ 讀、寫、儲存、刪除檔案
阿新 • • 發佈:2019-02-20
讀、寫儲存用 stream即可
讀:
包含#include <fstream>
CString file=_T("c:\\t.txt");
char buf[1024]; char* message; std::ifstream infile; infile.open(file); if(infile.is_open()) //檔案開啟成功,說明曾經寫入過東西 { //while(infile.good() && !infile.eof()) { memset(buf,0,1024); infile.getline(buf,1024); message = buf; } infile.close(); }
寫與儲存:
char pBuf[MAX_PATH]; //存放路徑的變數 GetCurrentDirectory(MAX_PATH,pBuf); //獲取程式的當前目錄 String ppt=pBuf; ppt=ppt+mPath; CString filename=ppt+"\\classRoom.xml"; std::ofstream fout; fout.open(filename); fout<<strInfo<<"\n"; fout<<std::flush; fout.close();
刪除檔案:
char pBuf[MAX_PATH]; //存放路徑的變數
GetCurrentDirectory(MAX_PATH,pBuf); //獲取程式的當前目錄
String ppt=pBuf;
CString filename=ppt+"\\st.inf";
DeleteFile((LPCTSTR)filename.c_str());