去掉文本文件中含有某些字符串的行

来源:互联网 发布:网络弊大于利攻辩问题 编辑:程序博客网 时间:2024/05/23 01:16

#include <fstream>

#include <io.h>

#include <stdio.h>

#include <string>

using namespace std;

char filepath[MAX_PATH]={"sktdll.log"};

charbuf[1024]={0};//one line

char strmsg[]={"<msg>hb</msg>"};

 

TCHARg_tbuf[MAX_PATH*4];

void DebugMsg(const TCHAR* pszFormat, ...)

{    

CTime now = CTime::GetCurrentTime();

    _stprintf(g_tbuf, _T("/n[%s]"), now.Format("%Y-%m-%d %H:%M:%S"));

va_list arglist;

va_start(arglist, pszFormat);

    _vstprintf(&g_tbuf[_tcslen(g_tbuf)], pszFormat, arglist);

va_end(arglist);    

    OutputDebugString(g_tbuf);

//logs(1,_T("%s"),g_tbuf);

}

 

ifstream infile;

infile.open(filepath);

if ( !infile.is_open() ) 

{

AfxMessageBox("ûÕÒµ½¸ÃÎļþ!");

return ;

}

string file_buf;

while( !infile.eof() )

{

memset(buf, 0, sizeof(buf));

infile.getline(buf, sizeof(buf));

//if ( buf[0] == 0 ) continue;

//if ( buf[0] == '#' ) continue;//comment

//if ( buf[0] == '/n' ) continue;

//if ( buf[0] == ' ' ) continue;

DebugMsg("%d :%s",strlen(buf) ,buf);

if (NULL==strstr(buf,strmsg))

{

file_buf.append(buf);

file_buf.append("/n");

}

}

infile.close();

if (file_buf.length()>0)

{

ofstream outfile;

outfile.open(filepath,ios::out);

outfile<<file_buf.c_str()<<endl; 

outfile.close();

AfxMessageBox("ok!");

}else{

AfxMessageBox("error!");

}

原创粉丝点击