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:
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
// using istringstream constructors.
#include <iostream>
#include <sstream>
#include <string>
usingnamespace 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.
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.