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


```
#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

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

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

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

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

提前感谢!

愿你安好

戴夫·

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

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

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 = ""; 
}

不会停止在"点 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)更容易阅读!

我没有检查代码下方是否有更多错误,但是通过中断,您肯定会找到正确的值。

第 2 部分

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

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;
}

相关内容

  • 没有找到相关文章

最新更新