当我进入循环时,我的“计数器”从1跳到4。有什么想法吗?代码和输出如下:
static bool harvestLog()
{
ifstream myFile("LOGS/ex090716.log");
if (myFile.fail()) {cout << "Error opening file";return 1;}
else
{
cout << "File opened... \n";
string line;
string field;
int cs_uri_stemLocation = 0;
int csReferrerLocation = 0;
int count = 1;
cout << "-" << count << "-";
while( getline(myFile, line) ) {
if ( strstr(line.c_str(), "cs-uri-stem") &&
(strstr(line.c_str(), "cs(Referer)") || strstr(line.c_str(), "cs(Referrer)")) )
{
cout << "-" << count << "-";
cout << "Found log format: \n";
istringstream foundField(line);
while (!foundField.eof())
{
cout << "-" << count << "-";
foundField >> field;
if (field == "cs-uri-stem") {cs_uri_stemLocation = count;}
if (field == "cs(Referer)" || field == "cs(Referrer)") {csReferrerLocation = count;}
cout << "cs-uri-stem: " << cs_uri_stemLocation << ". ";
cout << "cs(Referer): " << csReferrerLocation << ". ";
cout << "COUNT: " << count << endl;
count++;
}
cout << "Found field cs-uri-stem at position " << cs_uri_stemLocation << "." << endl;
cout << "Found field cs(Referer) at position " << csReferrerLocation << "." << endl;
count = 1;
}
else
{
count = 1;
istringstream foundField(line);
while (!foundField.eof())
{
foundField >> field;
//if (count == cs_uri_stemLocation) cout << field << endl;
count++;
}
//cmatch results;
//regex rx("(?:p|q)(?:=)([^ %]*)");
//regex_search(line.c_str(), results, rx);
//string referringWords = results[1];
//cout << referringWords;
}
}
myFile.close();
return 0;
}
}-1--4-找到日志格式:
-4-cs-uri-干: 0。政务司司长(推荐):0。计数:4
-5-cs-uri-干: 0。政务司司长(推荐):0。计数:5
-6-cs-uri-阀杆: 0。政务司司长(推荐):0。计数:6
-7-cs-uri-干: 0。政务司司长(推荐):0。计数:7
-8-cs-uri-干: 0。政务司司长(推荐):0。计数:8
-9-cs-uri-干: 0.政务司司长(推荐):0。计数:9
- 10 -cs-uri-茎:10。cs(推荐者):0。计数: 10
-11-cs-uri-茎: 10。cs(推荐者):0。计数: 11
-12-cs-uri-茎: 10。cs(推荐者):0。计数: 12
-13-cs-uri-茎: 10。cs(推荐者):0。计数: 13
-14-cs-uri-茎: 10。cs(推荐者):0。计数: 14
-15-cs-uri-茎: 10。cs(推荐者):0。计数: 15
- 16 -cs-uri-阀杆: 10。cs(推荐者):16。计数:16。
- 17 -cs-uri-阀杆: 10。cs(推荐):16。计数:17。
- 18 -cs-uri-阀杆: 10。cs(推荐):16。计数:18。
- 19 -cs-uri-阀杆: 10。cs(推荐):16。计数:19。
- 20 -cs-uri-阀杆: 10。cs(推荐):16。计数:20。
在10号位置找到了领域cs-uri-阀杆。
在位置16处找到cs字段(推荐者)。
发布于 2009-09-18 15:33:44
我敢打赌它一定会通过
while (!foundField.eof())
{
foundField >> field;
//if (count == cs_uri_stemLocation) cout << field << endl;
count++;
}你从来没有在这个分支之后重新设置它
发布于 2009-09-18 15:39:48
您不能附加调试器并逐步完成代码吗?看起来你不会有太多的迭代来找到答案。(如果此,则只需单击run即可在count上设置数据断点。我怀疑GDB和大多数其他调试器也会支持这一点。)
发布于 2009-09-18 15:31:12
您还没有在循环中提供所有代码-在while语句之后有一个无与伦比的打开大括号。你在提取的下面有一些cout代码吗?
https://stackoverflow.com/questions/1445237
复制相似问题