Reputation: 1373
i am trying to read tokenised data from a text file into a vector of pointers by overloading the operator>>() function in a custom class Customer. my code works fine to read them in through the whole file, but then i get a seg fault when it is finished
here is my code:
int line = 0;
vector<Customer *> customers;
ifstream fin("customers.txt", ios_base::in);
while (fin)
{
Customer *temp = new Customer();
line++;
try
{
fin >> *temp;
customers.push_back(temp);
}
catch(boost::bad_lexical_cast&)
{
cerr << "Bad data found at line " << line
<< " in file customers.txt" << endl;
}
}
assume that the overloaded operator>>() function works to read a line in with getline() and inserts the data into the temp pointer to Customer, throwing a bad_lexical_cast if any invalid data is found..
i realise i can change:
while (fin)
to:
while (fin >> *temp)
but i want to keep the try/catch block, as if bad data is found i just want it to skip that line and continue to the next one.
is there anything i can do to test if the next line is there without actually pulling it? similar to the java hasNextLine in the scanner class?
any help would be greatly appreciated
Upvotes: 1
Views: 103