C语言 C 的 printf 和 fprintf(stdout,) 不打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14784367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C's printf and fprintf(stdout,) are not printing
提问by Hector
This is a bit of an odd one. My code wasn't outputting what I thought it should. I added some print statements at various stages to see where it was going wrong. Still nothing. So I added a printf statement at the start of main. That's where I got really confused.
这有点奇怪。我的代码没有输出我认为应该的内容。我在各个阶段添加了一些打印语句,以查看哪里出错了。依然没有。所以我在main的开头加了一个printf语句。这就是我真正困惑的地方。
So I presumed something funny was happening with the file descriptors. I changed the printfto a fprintf. Still nothing. Printing to stderr with fprintfdoes work! Why is this happening?
所以我推测文件描述符发生了一些有趣的事情。我将其更改printf为fprintf. 依然没有。打印到 stderrfprintf确实有效!为什么会这样?
Removing all of the body from main except the initial print statement and the return does print.
从 main 中删除除初始打印语句之外的所有主体,并且返回确实打印。
Code
代码
int main(void) {
fprintf(stdout, "STARTED!");
//Create an Internet domain socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
//If this fails exit and print the error
if (sockfd == -1) {
printf("Error %d, cannot create socket", errno);
return 1;
}
printf("SOCKET CREATED!");
//Creates a socket address
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
//Attempts to bind to the socket address, again prints to error if this fails.
if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
{
printf("Error %d, cannot bind", errno);
return 1;
}
//Starts Listening for a client
if (listen(sockfd, 1) == -1)
{
printf("Error %d, cannot listen", errno);
return 1;
}
//If all is successful, server is operational
while(1)
{
//Creates a file descripter for the connection
int connfd;
//And a socket address for the client
struct sockaddr_in cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
//If a connection attempt is made accepts it.
connfd = accept(sockfd, (struct sockaddr *) &cliaddr, &cliaddrlen);
if (connfd == -1) {
//If the connection fails print an error
printf("Error %d, cannot accept connection", errno);
continue;
}
//Otherwise process the request
else {
printf("CONNECTED!");
char end;
end = 1;
while (end)
{
process_request(connfd);
end = 0;
}
}
close(connfd);
}
close(sockfd);
return 0;
}
回答by andrew cooke
Output is often buffered by the system. You can call fflush, but sometimes, depending on how the caching works, simply ending the output with a newline is sufficient. So try changing
输出通常由系统缓冲。您可以调用 fflush,但有时,根据缓存的工作方式,只需以换行符结束输出就足够了。所以尝试改变
fprintf(stdout, "STARTED!");
to
到
fprintf(stdout, "STARTED!\n");
And, if that doesn't help, to
而且,如果这没有帮助,
fprintf(stdout, "STARTED!\n");
fflush(stdout)
(And stderr often isn't cached, as you want to see errors immediately.)
(并且 stderr 通常不会被缓存,因为您希望立即看到错误。)
Finally, you will see output when the program finishes (as things are flushed then), which probably explains the rest of the behaviour.
最后,您将在程序完成时看到输出(因为事情已被刷新),这可能解释了其余的行为。

