Problem with istringstream

I'm working on a code to read in an input file and convert it to another format. I've created a function that reads in a line of text and converts it to a vector of strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::vector<std::string> CAR2D::ProcessLine( std::ifstream& input, std::vector<std::string> inpL)
{
	std::string line;
	inpL.clear();
	std::cout << inpL.size() << std::endl;
	getline(input,line,'\n');
		if( !line.empty() ) {
		std::string token;
		std::istringstream iss(line); // Gives Seg Fault
		while ( getline(iss,token,' ' ) ) {
			if( !token.empty() ) {
				inpL.push_back(token);
				
			}
		}
	}

	//Check for comments, and repeat if necessary
	if(inpL[0]=="*")
		ProcessLine(input, inpL);
	return inpL;
}


This code successfully runs several times, and then gives a Segmentation Fault. Any advice for why it's giving me a seg fault?
Line 19:
 
if(inpL[0]=="*")

You don't check if inpL[0] exists. Maybe try:
 
if(!inpL.empty() && inpL[0]=="*")

I'll try that but the seg fault didn't come from that line, and the line of the text file being read that the code fails on is not empty.
I tried clearing inpL before calling this function and it seems to fix it but I don't know why.
istringstream does not take std::string as constructor, the compiler will issue a warning in this case:
std::istringstream iss(line); // Gives Seg Fault



Please fix this and try again.
What do you mean it doesn't take std::string as a constructor? What else would it take?

This code works for an input text line:
MESHX 0.0 4.05003 MESHY 0.0 3.85445

but not for the line:
RADIUS 0.0 1.7411 1.7920
std::istringstream is typedefed as this, it takes onst char* as constructor.
1
2
typedef basic_istringstream<char, char_traits<char>,
	allocator<char> > istringstream;
But the example on the istringstream reference page says it also takes strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   
// using istringstream constructors.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main () {

  int n,val;
  string stringvalues;

  stringvalues = "125 320 512 750 333";
  istringstream iss (stringvalues,istringstream::in);

  for (n=0; n<5; n++)
  {
    iss >> val;
    cout << val*2 << endl;
  }

  return 0;
}


My question is why it my code works on some lines of the input text file, and not on others. I"m sorry if I'm being snappy. I'm just really frustrated with this code. Thanks for your help so far.
std::istringstream does take strings, not const char*

The only thing I can see is that you do not check if your read succeeded or not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::vector<std::string> CAR2D::ProcessLine( std::ifstream& input, std::vector<std::string> inpL)
{
	std::string line;
	inpL.clear();
	std::cout << inpL.size() << std::endl;
	getline(input,line,'\n'); // What happens if this fails?
		if( !line.empty() ) {
		std::string token;
		std::istringstream iss(line); // Gives Seg Fault
		while ( getline(iss,token,' ' ) ) {
			if( !token.empty() ) {
				inpL.push_back(token);
				
			}
		}
	}

	//Check for comments, and repeat if necessary
	if(inpL[0]=="*")
		ProcessLine(input, inpL);
	return inpL;
}

Perhaps the invalid contents of std::string line is somehow causing std::istringstream to trip up?
Last edited on
That's a good point, I'll put a flag to print an error if getline doesn't work.
Unfortunately, I checked that line is correct for the input text line that istringstring fails on :/

And for some reason, when I include another
inpL.clear()
right before the function call, it works. Which makes no sense to me, because it clear the inpL in the function itself.
Last edited on
Topic archived. No new replies allowed.