Thursday, 29 August 2013

stringstream sometimes won't reach the end of a file

stringstream sometimes won't reach the end of a file

I wrote a c++ program to manupilate data files. However in 5 files out of
100, the stringstream can't reach the end of the file. I ran tests within
the following chunk of code:
// ssFile contains the whole text in each file.
// chunk is a string.
// value is an int
// tId and tName are both string vectors.
loop = 0;
while(ssFile >> chunk)
{
if(ssFile.eof()) cout << "\nEOF!\n" << endl;
if( chunk == "<frame>" )
{
ssFile >> value;
cout << "-> " << loop << ": " << chunk << value;
{
stringstream ssVal;
ssVal << value;
tId.push_back(ssVal.str());
}
loop++;
ssFile >> chunk;
tName.push_back(chunk);
cout << "= " << chunk << endl;
}
}// while (ssFile >> chunk)
In these 5 particular files, the ssFile sstream reaches no end at all: the
loop is endlessly executed. I checked these txt files but their end
doesn't seem to be different from the others. Does it come from the
stringstreams (which I doubt)? Or more probably from these files? If so
how are end of files characters represented, perhaps these file are
missing one?
Edit: added an EOF test and the following output:
-> 1: <frame>1= flying
-> 2: <frame>2= flying
-> 3: <frame>3= flying
-> 4: <frame>4= flying
-> 5: <frame>5= flying
-> 6: <frame>10= hiting
-> 7: <frame>11= hiting
-> 8: <frame>12= hiting
-> 9: <frame>13= hiting
-> 10: <frame>20= hit
-> 11: <frame>21= hit
-> 12: <frame>22= hit
-> 13: <frame>23= hit
-> 14: <frame>30= rebounding
-> 15: <frame>31= rebounding
-> 16: <frame>32= rebounding
-> 17: <frame>33= rebounding
EOF!
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
Actually the loop is not executed again, the program just happens to be
stuck at the last occurence... So there is no infinite loop/output. THe
end of file seems to be detected, but still the program does not get out
of the loop. Then it tells me a bad allocation has been made.

No comments:

Post a Comment