0

当我进入循环时,我的“计数器”从 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-stem: 0. cs(Referer): 0. COUNT: 4
-5-cs-uri-stem: 0. cs(Referer): 0. COUNT : 5
-6-cs-uri-stem: 0. cs(Referer): 0. COUNT: 6
-7-cs-uri-stem: 0. cs(Referer): 0. COUNT: 7
-8-cs-uri -stem:0.cs(Referer):0.COUNT:8
-9-cs-uri-stem:0.cs(Referer):0.COUNT:9
-10-cs-uri-stem:10.cs(Referer ): 0. COUNT: 10
-11-cs-uri-stem: 10.cs(Referer): 0. COUNT: 11
-12-cs-uri-stem: 10.cs(Referer): 0. COUNT: 12
- 13-cs-uri-stem: 10.cs(Referer): 0. COUNT: 13
-14-cs-uri-stem: 10.cs(Referer): 0. COUNT: 14
-15-cs-uri-stem: 10.cs(推荐人):0。计数:15
-16-cs-uri-stem:10.cs(推荐人):16。计数:16
-17-cs-uri-stem: 10.cs(Referer): 16. COUNT: 17
-18-cs-uri-stem: 10.cs(Referer): 16. COUNT: 18
-19-cs-uri-stem : 10. cs(Referer): 16. COUNT: 19
-20-cs-uri-stem: 10. cs(Referer): 16. COUNT: 20
在位置 10
找到字段 cs-uri-stem。找到字段 cs(Referer ) 在第 16 位。

4

4 回答 4

6

我敢打赌它正在经历

                        while (!foundField.eof())
                        {
                                foundField >> field;
                                //if (count == cs_uri_stemLocation) cout << field << endl;
                                count++;
                        }

在这个分支之后你永远不会重置它

于 2009-09-18T15:33:44.023 回答
1

你不能附加一个调试器并单步执行代码吗?看起来您不会进行多次迭代才能看到答案。(如果这个 Visual Studio 你可以在点击运行时设置一个数据断点。我怀疑 GDB 和大多数其他调试器也会支持这一点。)

于 2009-09-18T15:39:48.567 回答
0

您没有在循环中提供所有代码 - 在 while 语句之后有一个不匹配的开放花括号。您在提取的内容下方是否有一些 cout 代码?

于 2009-09-18T15:31:12.453 回答
0

也许您的 if 语句在 while 循环开始后立即返回错误?附加到 if 的 else 包含循环中的 count++ 语句。

于 2009-09-18T15:40:20.383 回答