我已阅读整个文件到从存储器映射文件赢APIC++解析一个线路输出一个大文件的
CreateFile("WarandPeace.txt", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
等字符串...
每一行端接一个CRLF 。我需要在“我喜欢垃圾邮件和鸡蛋”一行中找到类似于“垃圾邮件”的行(并将整行(不含CRLF))返回到字符串中(或指向字符串中的位置的指针)。原始字符串不能改变
编辑:。
事情是这样的:
string ParseStr(string sIn, string sDelim, int nField)
{
int match, LenStr, LenDelim, ePos, sPos(0), count(0);
string sRet;
LenDelim = sDelim.length();
LenStr = sIn.length();
if(LenStr < 1 || LenDelim < 1) return ""; // Empty String
if(nField < 1) return "";
//=========== cout << "LenDelim=" << LenDelim << ", sIn.length=" << sIn.length() << endl;
for(ePos=0; ePos < LenStr; ePos++) // iterate through the string
{ // cout << "sPos=" << sPos << ", LenStr=" << LenStr << ", ePos=" << ePos << ", sIn[ePos]=" << sIn[ePos] << endl;
match = 1; // default = match found
for(int k=0; k < LenDelim; k++) // Byte value
{
if(ePos+k > LenStr) // end of the string
break;
else if(sIn[ePos+k] != sDelim[k]){ // match failed
match = 0; break; }
}
//===========
if(match || (ePos == LenStr-1)) // process line
{
if(!match) ePos = LenStr + LenDelim; // (ePos == LenStr-1)
count++; // cout << "sPos=" << sPos << ", ePos=" << ePos << " >" << sIn.substr(sPos, ePos-sPos) << endl;
if(count == nField){ sRet = sIn.substr(sPos, ePos-sPos); break; }
ePos = ePos+LenDelim-1; // jump over Delim
sPos = ePos+1; // Begin after Delim
} // cout << "Final ePos=" << ePos << ", count=" << count << ", LenStr=" << LenStr << endl;
}// next
return sRet;
}
如果你喜欢它,投上一票。如果没有,那么让我们来看看你得到了什么
CreateFile()没有内存映射文件。它只会打开它。之后你真的调用CreateFileMapping()和MapViewOfFile()吗? – 2009-11-24 14:38:31
另一个令人困惑的方面:如果你真的存储映射文件,为什么将它复制到一个字符串呢? – 2009-11-24 14:40:03
这是一个解析练习。但有些时候,你需要从内存映射文件中解析出线路,甚至是从HTTP请求中解析响应等。 – 2009-11-25 10:06:30