Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Qoy

Closing redirected output

This topic is 5988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If one redirects stdout via a call to freopen(), then does one have to close stdout at the end of the program, since it was reopened, or will the CRT still take care of closing it? I would assume that the CRT will close it after main returns no matter what device it''s directed to, but I wanted to make sure. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I don''t know if you have to but I do anyway since freopen() returns a file pointer and that means there''s an open file out there. It''s rare, but I think I''ve had instances of the os telling me that it couldn''t do something cause the file was in use(open, in other words) even though the program that used it had crashed.
Besides, it''s only an extra line fclose(fp);

Share this post


Link to post
Share on other sites
yes you should close it.

quote:

The freopen function opens the file whose name is the
string pointed to by path and associates the stream
pointed to by stream with it. The original stream (if it
exists) is closed. The mode argument is used just as in
the fopen function. The primary use of the freopen func-
tion is to change the file associated with a standard text
stream (stderr, stdin, or stdout).



being that the call closes the original stream, you are left to close the new one.

[edited by - a person on June 28, 2002 12:13:14 AM]

Share this post


Link to post
Share on other sites
Except that the CRT always has open streams in stdout, stdin, and stderr which it must close after main returns anyway... If it closes the streams pointed to by the three std files normally, it''s logical to think that it would probably close them even if you reopened them, since the file pointers are the same.

At any rate, the stream *does* end up closed because I can move or delete the file without any problem after the program has exited, which, as mentioned, you cannot do with a file that currently has a stream opened to it, and which I cannot do before the program has exited.

Share this post


Link to post
Share on other sites
quote:
Original post by Qoy
At any rate, the stream *does* end up closed because I can move or delete the file without any problem after the program has exited, which, as mentioned, you cannot do with a file that currently has a stream opened to it, and which I cannot do before the program has exited.

Windows will automatically close open handles when a process terminates (or at least it will in some circumstances). This doesn''t guarantee that fclose is actually called and CRT closes the handles. The only way to be sure of that is put a breakpoint in fclose and see if it''s hit when program exits.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!