c++记录日志到txt
int WriteLog(string msg) { SYSTEMTIME sys; GetLocalTime( &sys ); //取系统时间 CString strtemp; strtemp.Format("%d-%d-%d %d:%d:%d::%d :",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds); string dtime((LPCSTR)strtemp); ofstream outfile ; outfile.open("d:\\LOG.txt",ios::app); //文件的物理地址,文件的打开方式 int m=0; if(outfile.is_open()) { outfile<<dtime<<msg<<"\n"; outfile.close(); return 0; } else { return 1; } }
读取txt文件:
ifstream infile ("d:\\LOG.txt"); string line; if(infile) { while(getline(infile,line)) { cout << line << endl; } }