0

This is an exam question:

Considering the C++ program below, what should be inserted in place of //***** to ensure a 100% clean shutdown?

#include <iostream>
#include <fstream>

int main(int argc, char* argv[])
{
    using namespace std;

    fstream log("log.txt", ios::out);
    streambuf* clog_buf = clog.rdbuf(log.rdbuf());

    clog << "Test the logger" << endl;

    //*****
}
  • A. Nothing is missing.
  • B. exit();
  • C. clog.rdbuf(clog_buf);
  • D. clog.rdbuf(0);
  • E. log.rdbuf(0);

I am rather confused on the use of log and clog in this code. Why can't we just create a file and write down everything we need? Any explanation would be appreciated.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    You've got two (`clog` and `log`) streams both owning the same `streambuf`. What happens when the streams go out of scope? – jrok Dec 16 '13 at 22:48
  • 1
    Asking for the answer to an exam question (even if you've already handed your exam in) in the middle of exam season seems like a poor choice. Perhaps you could/should ask your instructor instead? – Caleb Dec 16 '13 at 22:48
  • Typical exam question. In real life you'd go to cppreference.com or check your Strousup book to make sure you used the API correctly. Because often when you believe you are doing it correctly, you aren't. – Zan Lynx Dec 16 '13 at 22:53
  • @datenwolf: You haven't corrected it yet. Both "the compiler will probably warn" and "a return statement is definitely missing" are untrue. `main` is a special case. – Lightness Races in Orbit Dec 16 '13 at 22:54
  • @datenwolf Returning from main is not identical to calling exit. – fizzer Dec 16 '13 at 22:54
  • The code runs very well on VS 2008 even though without return value. I think return value is the point of this question. – user3092410 Dec 16 '13 at 22:54
  • @fizzer: I'll just delete my comment. No so important anyway :) – datenwolf Dec 16 '13 at 22:56
  • @user3092410: I'm pretty sure it's not the point of this question, not least because _none_ of the available answers say anything at all about a return value. – Lightness Races in Orbit Dec 16 '13 at 22:57

3 Answers3

2

clog_buf points to the stream buffer that clog pointed to before you reset it with rdbuf. A clean shutdown can be achieved by resetting clog's stream buffer to what it was before by using C ( clog.rdbuf( clog_buf ); ).

wrren
  • 1,281
  • 9
  • 11
  • Thank you for your answer. But on VS 2008, the code without "clog.rdbuf(clog_buf);" runs well. How do I test if it is a clean shutdown? – user3092410 Dec 16 '13 at 22:59
  • You can call 'clog << "Test Message";' and see if it appears in your terminal as expected. – wrren Dec 17 '13 at 12:33
2

The code is swapping the standard clog stream (which initially works on STDERR) for a file stream. It means that any code anywhere in the program that streams to clog will actually now stream to a file instead. It's a good localised way to redirect log output, which doesn't require search/replacing ten million utterances of the text clog in your source code.

The answer is C, which reverts the clog stream back to the way you found it before the implicit return 0 kicks in and the program ends gracefully, including the normal file stream destruction.

exit() is a non-graceful shutdown which would not close your file stream properly.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

This code does not appear to be exception safe. Therefore, in the face of exceptions, none of the answers will ensure a 100% clean shutdown.

YoungJohn
  • 946
  • 12
  • 18