2

My code set std::failbit when it reach eof and exception is thrown how can i skip eof exception

In catch block i check and skip if exception is because of eof but its not good.

please suggest how can i skip eof exception in below code

std::ifstream in
std::string strRead;
in.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
try
{

while (getline( in,strRead))

{
         //reading the file
 }

 catch(std::ifstream::failure & exce)
{
         if(! in.eof())   // skip if exception because of eof but its not working?
        {
               cout<<exce.what()<<endl;
                return false;
        }


}
catch(...)
{

        cout("Unknow  exception ");
        return false;
}
leuage
  • 566
  • 3
  • 17
Rohini Singh
  • 297
  • 1
  • 14
  • Possible duplicate of [std::getline throwing when it hits eof](https://stackoverflow.com/questions/11807804/stdgetline-throwing-when-it-hits-eof) – Cristian Ionescu Jul 19 '17 at 13:21
  • 1
    @CristianIonescu No its not please read my till end question my concern is whether I can handle in catch block ? – Rohini Singh Jul 19 '17 at 13:27
  • @CristianIonescu there is no where related to my question – Rohini Singh Jul 19 '17 at 13:28
  • Please provide [MVCE](https://stackoverflow.com/help/mcve). – Cristian Ionescu Jul 19 '17 at 13:44
  • @CristianIonescu the link which u shared no where talks about handling the eof exception in catch block where my code does and this is what all my question is.i request you please read all my question and compare with the one you share – Rohini Singh Jul 19 '17 at 13:50
  • if you refer to `if(!folderContent.eof())` this does not work because you read from **in**, folderContent is nowhere present in your sample. That's why I asked for a [MVCE](https://stackoverflow.com/help/mcve). – Cristian Ionescu Jul 19 '17 at 14:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149617/discussion-between-cristian-ionescu-and-rohini-singh). – Cristian Ionescu Jul 19 '17 at 14:09

2 Answers2

1

Disable the failbit will be the best approach else every time you will get this exception when( while getline() ) is used

leuage
  • 566
  • 3
  • 17
1

After discussions in private, we managed to find a solution for his problem: getline(in, strRead) would set the failbit to 1 when eof is reached( normal behavior) and he didn't want that to happen.We agreed to use other method to read the content of the file:

std::ifstream in(*filename*); // replace *filename* with actual file name.
// Check if file opened successfully.
if(!in.is_open()) {
       std::cout<<"could not open file"<<std::endl;
       return false;
}

in.seekg(0, std::ios::end);
std::string strRead;

// Allocate space for file content.
try {
    strRead.reserve(static_cast<unsigned>(in.tellg()));
} catch( const std::length_error &le) {
    std::cout<<"could not reserve space for file"<<le.what()<<std::endl;
    return false;
} catch(const std::bad_alloc &bae) {
    std::cout<<"bad alloc occurred for file content"<<bae.what()<<std::endl;
    return false;
} catch(...) {
    std::cout<<"other exception occurred while reserving space for file content"<<std::endl;
    return false;
}

in.seekg(0, std::ios::beg);
// Put the content in strRead.
strRead.assign(std::istreambuf_iterator<char>(in),
        std::istreambuf_iterator<char>());
// Check for errors during reading.
if(in.bad()) {
    std::cout<<"error while reading file"<<std::endl;
    return false;
}

return true;
Cristian Ionescu
  • 181
  • 3
  • 10