c++ - 为什么 int count 在进入循环时从 1 跳到 4? C++

标签 c++ visual-studio loops counter increment

当我进入循环时,我的“计数器”从 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-Found log format:
-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(Referer): 0. COUNT: 15
-16-cs-uri-stem: 10. cs(Referer): 16. COUNT: 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
Found field cs-uri-stem at position 10.
Found field cs(Referer) at position 16.

最佳答案

我敢打赌它会经历

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

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

关于c++ - 为什么 int count 在进入循环时从 1 跳到 4? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1445237/

相关文章:

c++ - 将 istream 运算符 >> 转换为 istream getline

c++ - 对象数组的变量类型

loops - 是否有展开或继续循环的快捷方式?

python - 如何将表中特定列的每一行的长度与支持表中的特定值相对应,并在满足条件时创建标志?

java - 遍历 Java 字符串行的最佳方法是什么?

c++ - 关于 map STL的问题

c++ - QNetworkReply 在发出完成信号时抛出 SIGSEGV

c# - Visual Studio : can't find "resource file" in list of items to add to project

asp.net-mvc - 可浏览的网站同时受版本控制?

c++ - 如何实现综合基准?