小编pga*_*iel的帖子

为什么stringstream会停止接收字符串?[C++]

我正在尝试实现一种从文本文件中读取输入以更轻松地加载不同坐标集的方法,但是我遇到了一个我不理解的错误,stringstream一旦其中一行是我的对象将停止接收字符串生病格式.

在我的输出中,您可以看到字符串在打印出来时仍然完好无损,然后它被放入stringstream下一行,但是在一个格式不正确的字符串之后,stringstream当我将其打印出来时,停止包含任何内容.

这里发生了什么?

输出:

图片

这是文本文件的样子:

图片

方法代码:

ifstream pathfile(p.string());
cout << "Path file opened successfully.\n\n";

string line;
stringstream ss;
int x, y;
char comma,direction;

//Iterate all lines in the file
while(getline(pathfile,line)){
  //remove all spaces from line
  line.erase(remove(line.begin(), line.end(), ' '), line.end());
  //skip comments and blank lines
  if(line.c_str()[0] == '#' || line.empty()) continue;
  //parse remaining lines
  ss.str(string()); //clear stringstream
  cout <<"LINE: "<<line<<endl;
  ss << line;
  cout <<"SS: "<<ss.str()<<endl;

  if(ss >> x >> comma >> y >> …
Run Code Online (Sandbox Code Playgroud)

c++ string stringstream ifstream c++11

3
推荐指数
1
解决办法
68
查看次数

标签 统计

c++ ×1

c++11 ×1

ifstream ×1

string ×1

stringstream ×1