Skip to main content
Describe what the code does, rather than the concerns with it
Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

Implementation using fstream failed evaluation Open, write and close a file

Question Unprotected by Simon Forsberg
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
deleted 16 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

My C++ code involving an Implementation using fstream failed reviewevaluation

I have a small 10-liner function that writes some data to a file using an std::ofstream. I did not explicitly call .close() at the end of my function, but it failed code review with the reason that it is better to explicitly call it for style and verbosity reasons. I understand there is no harm in calling .close() explicitly, but does calling it explicitly just before a return statement indicate a lack of understanding or faith in RAII? 

The C++ standard says:

§27.8.1.2

virtual ~ basic_filebuf ();

[3] Effects: Destroys an object of class basic_filebuf<charT,traits>. Calls close().

Am I justified in my argument that calling .close() at the end of a function is redundant and/or unnecessary?

###Edit for Code:

bool SomeClass::saveData()
{
    std::ofstream saveFile(m_filename);
    
    if (!saveFile.is_open())
        return false;
    
    saveFile << m_member1 << std::endl;
    saveFile << m_member2 << std::endl;
    
    saveFile.close(); // passed review only with this line
    return true;
}

The function is only supposed to return falsefalse if the file could not be opened for writing.

My C++ code involving an fstream failed review

I have a small 10-liner function that writes some data to a file using an std::ofstream. I did not explicitly call .close() at the end of my function, but it failed code review with the reason that it is better to explicitly call it for style and verbosity reasons. I understand there is no harm in calling .close() explicitly, but does calling it explicitly just before a return statement indicate a lack of understanding or faith in RAII? The C++ standard says:

§27.8.1.2

virtual ~ basic_filebuf ();

[3] Effects: Destroys an object of class basic_filebuf<charT,traits>. Calls close().

Am I justified in my argument that calling .close() at the end of a function is redundant and/or unnecessary?

###Edit for Code:

bool SomeClass::saveData()
{
    std::ofstream saveFile(m_filename);
    
    if (!saveFile.is_open())
        return false;
    
    saveFile << m_member1 << std::endl;
    saveFile << m_member2 << std::endl;
    
    saveFile.close(); // passed review only with this line
    return true;
}

The function is only supposed to return false if the file could not be opened for writing.

Implementation using fstream failed evaluation

I have a small 10-liner function that writes some data to a file using an std::ofstream. I did not explicitly call .close() at the end of my function, but it failed code review with the reason that it is better to explicitly call it for style and verbosity reasons. I understand there is no harm in calling .close() explicitly, but does calling it explicitly just before a return statement indicate a lack of understanding or faith in RAII? 

The C++ standard says:

§27.8.1.2

virtual ~ basic_filebuf ();

[3] Effects: Destroys an object of class basic_filebuf<charT,traits>. Calls close().

Am I justified in my argument that calling .close() at the end of a function is redundant and/or unnecessary?

bool SomeClass::saveData()
{
    std::ofstream saveFile(m_filename);
    
    if (!saveFile.is_open())
        return false;
    
    saveFile << m_member1 << std::endl;
    saveFile << m_member2 << std::endl;
    
    saveFile.close(); // passed review only with this line
    return true;
}

The function is only supposed to return false if the file could not be opened for writing.

Question Protected by Malachi
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 475 characters in body
Source Link
dreamlax
  • 1.2k
  • 1
  • 7
  • 10
Loading
Tweeted twitter.com/#!/StackCodeReview/status/32722977657196544
Source Link
dreamlax
  • 1.2k
  • 1
  • 7
  • 10
Loading