MOHAMED
MOHAMED

Reputation: 43518

strange behaviour when closing stdout

I have a program. In this program I have a http session with oher equipment. and in the program I have many printf. when I close the stdout with fclose(stdout) I remark that messages printed by the printf are send to the http session (messages included in the http packets).

I m wondering about this strange behvior and I m wondering How to avoid that behavior ? I mean how to avoid the redirect of the printed messages to the http session ?

Upvotes: 0

Views: 149

Answers (2)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215259

Calling printf after fclose(stdout) invokes undefined behavior. Don't do it. If you just need to inhibit stdout, open /dev/null and dup2 it onto fd #1.

Upvotes: 3

user149341
user149341

Reputation:

What's happening is that you're closing the file descriptor used by stdout, and the next file descriptor created (a socket, in this case) is ending up at the file descriptor traditionally used by it (FD 1).

Don't close stdout. If you need to make it go away, replace it with a file descriptor pointed to /dev/null, e.g:

freopen("/dev/null", "w", stdout);

Upvotes: 1

Related Questions