[英]c++ not writing to console unless a newline was encountered
I was recently writing a simple c++ program involving sockets. 我最近正在编写一个涉及套接字的简单c ++程序。 It had been a while since I worked with c++, so i was doing some simple sanity checking to make sure my classes were properly constructed.
自从我使用c ++以来已经有一段时间了,所以我进行了一些简单的健全性检查,以确保正确构造了我的类。 I encountered a very strange error then.
那我遇到了一个非常奇怪的错误。 When I did not have
\\n
or endl
at the end of my output to console, it would not write to console. 当我在输出到控制台的末尾没有
\\n
或endl
时,它将不会写入控制台。 For example: 例如:
This would not output to console 这不会输出到控制台
class Server{
public:
Server(){
std::cout << "STARTING SERVER";
}
};
This would: 这个会:
class Server{
public:
Server(){
std::cout << "STARTING SERVER" << std::endl;
}
};
Both were created using Server server;
两者都是使用
Server server;
创建的Server server;
. 。 Was this just a 'ghost' in my computer or has anyone encountered this before?
这只是我计算机中的“鬼”,还是以前有人遇到过?
That's normal. 那很正常 It's stored in the buffer until the buffer is flushed.
它存储在缓冲区中,直到刷新缓冲区。 You can send
std::flush
to the stream to flush it. 您可以将
std::flush
发送到流中以对其进行刷新。 You can use std::endl
to write an end of line and flush the buffer. 您可以使用
std::endl
写入行尾并刷新缓冲区。 It's basically an optimization to avoid large numbers of I/O operations if you write lots of small things to a stream. 从本质上讲,这是一种优化,可以避免在向流中写入很多小东西时避免进行大量I / O操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.