C++:使用stringstream从字符串中提取所需的整数后,获取两个字符串定界符和值之间的数字

C++: Get numbers between two string delimiters and values after extracting desired integer from string using stringstream

    ```
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <fstream>

    using namespace std;


    bool findCurrentNumber (int currentNumber, int foundNumber) 
    {
        if (currentNumber == foundNumber) 
        {
            return true;
        }
        return false;
    }


    int main()
    {

        ifstream inputFile;
        inputFile.open("C:\Projects\data.txt");
        if (!inputFile) 
        {
            cerr << "Error reading file "; 
        }
        else 
        {
        int searchCurrentNumber;
        cout << "Please type in the number you are looking for: ";
        cin >> searchCurrentNumber;

        bool foundRelevantSection = false;
        string delimiter = "energy";
        size_t pos = 0;
        string token;
        string line;


        while (getline(inputFile, line)) 
        {
             while ((pos = line.find(delimiter)) != string::npos) 
             {
                    token = line.substr(0, pos);
                    //check the found token
                    //cout << token << endl;
                    line.erase(0, pos + delimiter.length());
                    stringstream ss;     
                    //store the whole string into stringstream
                    ss << token; 
                    string temp; 
                    int found; 
                    while (!ss.eof()) 
                    { 
                    //extract the word by word from stream
                    ss >> temp; 
                    //Check the given word is integer or not
                    if (stringstream(temp) >> found) 
                    cout << "Number found: " << found << " " << endl;; 
                    //no space at the end of string
                    temp = ""; 
                    }
                    if (findCurrentNumber (searchCurrentNumber, found) == true)
                    {
                        while (getline (inputFile, line)) 
                        {
                        if (foundRelevantSection) 
                            {
                                //if no matches were found, the function returns "string::npos"
                                if(line.find("total") != string::npos) 
                                {
                                    //relevant section ends now
                                    foundRelevantSection = false;   
                                }
                                else    
                                {
                                cout << line << endl;
                                }
                            }
                            else 
                            {
                                if (line.find("point") != string::npos )
                                {
                                    foundRelevantSection = true;
                                }
                            }

                        }
                    }               
                    else 
                    {
                    cout << "The number is not equal on this line compared to what is typed in!" << endl;
                    }
              }                     
        } //closes the while-loop
    } //closes the if/else-statement

    inputFile.close();

        return 0;
    }
```

大家好,

我想解析具有这种格式的输入文件:

point     152 # energy  # 0.5
152 152 152 152 152 152
152 152 152 152 152 152
total  0.011  0.049  0.035 

point     153 # energy  # 1.5
153 153 153 153 153 153
153 153 153 153 153 153
total  0.015  0.050  0.040

该代码接受用户提供的整数并将其与从中提取的数字进行比较。字符串 "point 152 # energy"。如果用户输入数字“152”,代码应给出以下数字:

output: 
152 152 152 152 152 152
152 152 152 152 152 152

不幸的是,如果输入数字 152 或输入数字 153,则我的代码 returns 完全相反,不返回任何值。

任何人都可以帮助我并告诉我我做错了什么吗?我很感激任何提示!

提前致谢!

祝福,

戴夫

修复最后添加的第二个错误。

你应该努力改进调试器,我发现了你的一个问题:

while (!ss.eof()) 
    { 
    //extract the word by word from stream
    ss >> temp; 
    //Check the given word is integer or not
    if (stringstream(temp) >> found) 
         cout << "Number found: " << found << " " << endl;; 
    //no space at the end of string
    temp = ""; 
    }

在 "point 152 # " 中找到 152 后并没有停止,而是继续处理将找到的 # 变为 0。

这段带有中断的代码修复了该部分:

while (!ss.eof()) 
    { 
    //extract the word by word from stream
    ss >> temp; 
    //Check the given word is integer or not
    if (stringstream(temp) >> found) 
         {
         cout << "Number found: " << found << " " << endl;
         foundRelevantSection = true;
         break; /* exits the while now */
         }
    //no space at the end of string
    temp = ""; 
    }

或者您可以测试一下发现,首先将其设置为 0,然后使用 && found == 0

进行测试

然后调用 findCurrentNumber (int currentNumber, int foundNumber) 的部分是垃圾(或者更复杂的东西的占位符?)因为 if (findCurrentNumber (searchCurrentNumber, found) == true) 只是 if (searchCurrentNumber == found) 这更容易阅读!

我没有检查代码后面是否还有更多错误,但有了中断,您肯定会在 found 中得到正确的值。

第 2 部分

已经找到了"point",所以你不应该再找了!
我在上面的代码中添加了 foundRelevantSection,在 break 之前。
将下一部分更改为(如果相关则否,如果找到点则否):

while (getline (inputFile, line) && foundRelevantSection) 
    {
    //if no matches were found, the function returns "string::npos"
    if(line.find("total") != string::npos) 
        {
        //relevant section ends now
        foundRelevantSection = false;   
        }
    else    
        {
        cout << line << endl;
        }
    }

希望这是最后一个错误...

我检查了你的代码,虽然不是直截了当,但我至少能够理解你想要实现的目标。无论如何,这是我自己对你的问题的贡献。我希望它能帮助您或至少让您更好地了解解决方案

#include <iostream>
#include <fstream>
#include <regex>

int main()
{
    using namespace std;

    ifstream inputFile;

    inputFile.open("sample.dat");

    if (!inputFile)
    {
        cerr << "Error reading file ";
    }
    else
    {
        int searchCurrentNumber;
        bool foundRelevantSection = false;

        cout << "Please type in the number you are looking for: ";
        cin >> searchCurrentNumber;

        //while we are still having data to read
        std::string currentLine;
        while (std::getline(inputFile, currentLine))
        {
            //remove the top and bottom section of the log 
            if((currentLine.find("point") == string::npos) && (currentLine.find("total") == string::npos ))
            {
                //now all the numbers are in the middle then search for the number
                size_t pos = currentLine.find(std::to_string(searchCurrentNumber));
                if(pos!= string::npos){
                    //we found the number so we are in the section hopefully
                    string line;
                    while (std::getline(inputFile, line)) 
                        {
                        if (foundRelevantSection) 
                            {
                                //if no matches were found, the function returns "string::npos"
                                if(line.find("total") != string::npos) 
                                {
                                    //relevant section ends now
                                    foundRelevantSection = false;   
                                }
                                else    
                                {
                                cout << line << endl;
                                }
                            }
                            else 
                            {
                                if (line.find("point") != string::npos )
                                {
                                    foundRelevantSection = true;
                                }
                            }

                        }
                }
            }
        }
    }

    return 0;
}