When must the output stream in C++ be flushed?

后端 未结 6 1093
执笔经年
执笔经年 2021-01-21 21:45

I understand cout << \'\\n\' is preferred over cout << endl; but cout << \'\\n\' doesn\'t flush the output stream. When

相关标签:
6条回答
  • 2021-01-21 22:11

    First, you read wrong. Whether you use std::endl or '\n' depends largely on context, but when in doubt, std::endl is the normal default. Using '\n' is reserved to cases where you know in advance that the flush isn't necessary, and that it will be too costly.

    Flushing is involved with buffering. When you write to a stream, (typically) the data isn't written immediately to the system; it is simply copied into a buffer, which will be written when it is full, or when the file is closed. Or when it is explicitly flushed. This is for performance reasons: a system call is often a fairly expensive operation, and it's generally not a good idea to do it for every characters. Historically, C had something called line buffered mode, which flushed with every '\n', and it turns out that this is a good compromize for most things. For various technical reasons, C++ doesn't have it; using std::endl is C++'s way of achieving the same results.

    My recommendation would be to just use std::endl until you start having performance problems. If nothing else, it makes debugging simpler. If you want to go further, it makes sense to use '\n' when you're outputting a series of lines in just a few statements. And there are special cases, like logging, where you may want to explicitly control the flushing.

    0 讨论(0)
  • It depends on what you are doing. For example, if you are using the console to warn the user about a long process... printing a series of dots in the same line... flushing can be interesting. For normal output, line per line, you should not care about flushing.

    So, for char based output or non line based console output, flushing can be necessary. For line based output, it works as expected.

    This other answer can clarify your question, based on why avoiding endl and flushing manually may be good for performance reasons: mixing cout and printf for faster output

    Regarding what is flushing: when you write to a buffered stream, like ostream, you don't have any guarantee that your data arrived the destination device (console, file, etc). This happens because the stream can use intermediary buffers to hold your data and to not stop your program. Usually, if your buffers are big enough, they will hold all data and won't stop your program due to slow I/O device. You may have already noticed that the console is very slow. The flush operation tells the stream that you want to be sure all intermediary data arrived on the destination device, or at least that their buffers are now empty. It is very important for log files, for example, where you want to be sure (not 100%) a line will be on disk not in an buffer somewhere. This becomes more important if your program can't loose data, i.e., if it crashes, you want to be sure you did you best to write your data on disk. For other applications, performance is more important and you can let the OS decide when to flush buffers for you or wait until you close the stream, for example.

    0 讨论(0)
  • 2021-01-21 22:21

    Flushing can be disastrous if you are writing a large file with frequent spaces.

    For example

    for(int i = 0 ;i < LARGENUMBER;i++)
    {//Slow?
      auto point = xyz[i];
      cout<< point.x <<",",point.y<<endl;
    }
    

    vs

    for(int i = 0 ;i < LARGENUMBER;i++)
    {//faster
      auto point = xyz[i];
      cout<< point.x <<",",point.y<<"\n";
    }
    

    vs

    for(int i = 0 ;i < LARGENUMBER;i++)
    {//fastest?
      auto point = xyz[i];
      printf("%i,%i\n",point.x,point.y);
    }
    

    endl() was often know for doing other things, for example synchronize threads when in a so-called debug mode on MSVC, resulting in multithreaded programs that, contrary to expectation, printed uninterrupted phrases from different threads.

    0 讨论(0)
  • 2021-01-21 22:24

    Flushing forces an output stream to write any buffered characters. Read streamed input/output.

    It depends on your application, in real-time or interactive applications you need to flush them immediately but in many cases you can wait until closing the file and leave the program to flush it automatically.

    0 讨论(0)
  • 2021-01-21 22:26

    I/O libraries buffer data sent to stream for performance reasons. Whenever you need to be sure data has actually been sent to stream, you need to flush it (otherwise it may still be in buffer and not visible on screen or in file).

    Some operations automatically flush streams, but you can also explicitly call something like ostream::flush.

    You need to be sure data is flushed, whenever for example you have other program waiting for the input from first program.

    0 讨论(0)
  • 2021-01-21 22:28

    When must the output stream in C++ be flushed?

    When you want to be sure that data written to it is visible to other programs or (in the case of file streams) to other streams reading the same file which aren't tied to this one; and when you want to be certain that the output is written even if the program terminates abnormally.

    So you would want to do this when printing a message before a lengthy computation, or for printing a message to indicate that something's wrong (although you'd usually use cerr for that, which is automatically flushed after each output).

    There's usually no need to flush cerr (which, by default, has its unitbuf flag set to flush after each output), or to flush cout before reading from cin (these streams are tied so that cout is flushed automatically before reading cin).

    If the purpose of your program is to produce large amounts of output, either to cout or to a file, then don't flush after each line - that could slow it down significantly.

    What exactly is flushing?

    Output streams contain memory buffers, which are typically much faster to write to than the underlying output. Output operations put data into the buffer; flushing sends it to the final output.

    0 讨论(0)
提交回复
热议问题