0
votes
int DataSource::insertData(char* fileName){
double vel, tireAngle, timeStamp;
string inputLine;
int i=0;
ifstream inFile;

inFile.open(fileName);


if (!inFile.good())
{
    cout << "Could not open file " << fileName << endl;
    return 0;
}

while(inFile.good())
{
    getline(inFile, inputLine);

    if(inFile.fail())
    {
        break;
    }

    istringstream lineStream(inputLine);
    lineStream >> timeStamp >> vel >> tireAngle;

    source.push_back(Input(vel, tireAngle, timeStamp));
    ++i;
}

inFile.close();

return i;
};

I'm trying to pass in the file name, open the file and parse through each line. The Input class holds the three double values, and source is a vector of type Input. I have vector, iostream,fstream,string,sstream,climits #included, along with std namespace.

So my first issue is the file not opening, I know that the right filename is being passed in. The error I'm getting is:

"Could not open file input1.txt libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector"

For the vector, I googled the error and I think I'm accessing a bad vector, which is either because maybe I haven't properly initialized the vector -Input- source (formatting wont let me put the "greater than, less than sign") or I'm using the wrong syntax.

This is my first post so I'm sorry if this is a bad question/formatting, but any help would be appreciated.

1

1 Answers

0
votes

I don't think you should be using good() to check if the input is valid. Try changing

if (!inFile.good())

to

if (!inFile.is_open())

and changing

while(inFile.good())
{
    getline(inFile, inputLine);

to

while(getline(inFile, inputLine))