首页 > 解决方案 > C ++:使用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,我的代码将返回完全相反的结果,则不返回任何值。

谁能帮助我并告诉我我做错了什么?我很感激任何提示!

提前致谢!

最好的祝愿,

戴夫

标签: getlinestringstream

解决方案


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

你应该努力使用调试器变得更好,我发现你的问题是:

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)

我没有检查代码后面是否有更多错误,但是通过 break 你肯定会得到正确的值。

第2部分

已经找到了“点”,所以你不应该再找它!
我在上面的代码中添加了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;
        }
    }

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


推荐阅读