I was using the form used in one of the related questions. Only problem is that i keep getting right at the end of the file.
The file is an fstream and the str is a string.
Unhandled exception Microsoft C++ exception: std::ios_base::failure
while (getline(file, str)) 
{
}
if (cin.bad()) {
    // IO error
} else if (!cin.eof()) {
    // format error (not possible with getline but possible with operator>>)
} else {
    // format error (not possible with getline but possible with operator>>)
    // or end of file (can't make the difference)
}