开发者

close an unopened stream

I have ifstream and an ofstream that in runtime might be opened or not (depends on what the user enters in command line. i declare the variables 开发者_开发问答anyway, and i have a method that opens the stream if needed. my problem is at the end of the program i don't know if i need to close them or not.

Is there anyway in c++ to know if a stream was opened? Like in Java you can give a stream the null value and then ask if its null (it means that it was never opened)..

Is it ok to close a stream that was never opened?

this is the code:

int main(int argc, char* argv[]) {

    static std::ifstream ifs;
    static std::ofstream ofs;

        //might or might not open the streams:
    OpenStreams(ifs,ofs,argc-1,argv);
        ........

        //here i would like to close the streams at the end of the program
        //or not (if they were not opened

    return 0;

}

Thanks!


I don't really know, nor care to look. Just leave it to the destructors, the standard file streams will close the files during destruction if needed.

EDIT: On lifetimes of objects and guaranteed destruction...

To follow up the second comment to Ben Voigt that I wrote, this is a small test on object lifetimes:

#include <cstdlib>
#include <iostream>
#include <string>

struct test {
    std::string name;
    test( std::string const & name ) : name(name) {
        std::cout << "test " << name << std::endl;
    }
    ~test() { std::cout << "~test " << name << std::endl; }
};

void foo( bool exit ) {
    test t1( "1" );
    static test t2( "2" );
    test t3( "3" );
    if ( exit ) {
        std::exit(1);
    }
}
int main()
{
    foo(false);
    std::cout << std::endl;
    foo(true);
}

And the result of the execution:

test 1
test 2
test 3
~test 3
~test 1

test 1
test 3
~test 2

It can be seen that during the first execution the construction of the objects in foo occurs in the same order as the code., but when the function exits only the objects with auto storage get destroyed, with the object with static storage outliving the function execution.

In the second call to foo, the auto objects get recreated, while the static object doesn't, as expected, since it was created in a previous call. Because foo calls exit() in the second call, the auto objects in foo don't get destructed. On the other hand, the static object is correctly destructed.


Why not just test this using is_open() before you issue the close()?


No close call needed - the streams close itself when they are open when they are destroyed. Also, the static there looks suspicious. main is called only once, so it doesn't have any effect here (apart from pedantic standardese differences that don't matter here, i think.... Definitely not in the case shown).

That said, you can just call close if a stream is not opened - close will return a null pointer if it wasn't open. (I was looking at the spec for basic_filebuf<>::close - the file streams's close returns void).

File-streams can also handle non-open streams: If the stream wasn't open, it sets the failbit. You can check for that using fail() (which tests whehter the failbit or badbit is set). But there is is_open anyway to test whether the stream is open, but you don't need it for the above reasons.


Just don't make the variables static, that way they automatically close when main() returns.


You can use the is_open method to test if a stream has been opened, then close it.


Why not set a flag before opening a stream. Check the flag again if you need to close the stream object if any.

Better would be to pass that open stream object to the flag while opening a stream & use it to close the stream. If the flag has not been initialized or is null don't close.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜