//使用指定过滤规则 BOOL CFireView::ImplementRule(void) { HANDLE _hFile; DWORD error,nbytesRead; char data; CString _buff = ""; _hFile = CreateFile("saved.rul", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL); if(_hFile == INVALID_HANDLE_VALUE) { error = GetLastError(); MessageBox("Unable to open the file"); return FALSE; } else { BOOL bResult; do{ bResult = ReadFile(_hFile,&data,1,&nbytesRead,NULL); if((data != '\n')) { _buff = _buff + data; } else if((bResult && nbytesRead) !=0) { _buff.Remove('\n'); ParseToIp(_buff); _buff = ""; } }while((bResult && nbytesRead) !=0); CloseHandle(_hFile); } // Your code return TRUE; }
//使用指定过滤规则 BOOL CFireView::ImplementRule(void) { HANDLE hfile; //打开文件 hfile = CreateFile("saved.rul", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, //读写共享文件 NULL, OPEN_EXISTING, // 打开文件,且文件必须已经存在 NULL, NULL); if(hfile == INVALID_HANDLE_VALUE){ MessageBox("Can not open the file"); CloseHandle(hfile); return FALSE; } else{ int result; DWORD nbytesRead; char ipBuff; CString _buff = ""; //每次读取一个字符nbytesRead,暂存为ipBuff result = ReadFile(hfile, &ipBuff, 1, &nbytesRead, NULL); while(result && nbytesRead != 0){ //将每行数据存为_buff if(ipBuff != '\n') _buff += ipBuff; else{ //MessageBox(_buff); ParseToIp(_buff); _buff = ""; } result = ReadFile(hfile, &ipBuff, 1, &nbytesRead, NULL); } CloseHandle(hfile); return TRUE; } }