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

原文链接: https://www.cnblogs.com/bweb/p/5069884.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/226292

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午1:08
下一篇 2023年2月13日 下午1:08

相关推荐