Ask Your Question

printf output is too late

asked 2013-05-18 05:49:02 -0500

andrempunkt gravatar image

updated 2013-05-18 07:09:22 -0500

Hi, i have the following code:

#include "highgui.h"
#include "stdio.h"
using namespace std;

const char* name="Example1";

int main(int argc, char** argv) {
    IplImage* img = cvLoadImage(argv[1]);
    cvNamedWindow( name, CV_WINDOW_AUTOSIZE);
    cvShowImage( name, img);
    cvDestroyWindow (name);

It is all working but the "HelloWorld" is not written until I press a key. I don't understand why it is not written before.

ciao, Andre

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-05-18 05:56:03 -0500

Guanta gravatar image

The reason is that stdout is buffered. If you want to print it directly, write to stderr instead of stdout (if you really want to use C instead of C++ this would work with fprintf(stderr, "HelloWorld...")); or flush the buffer, should be working with a simple newline at the end: "HelloWorld\n".

Note: I highly recommend to switch to C++! OpenCV has mainly switched to it and you will have it a lot easier w. coding (std::cerr and std::cout ftw, and of course cv::Mat instead of IplImage/CvMat)!

edit flag offensive delete link more


thanks for the fast answer, worked perfect :)

andrempunkt gravatar imageandrempunkt ( 2013-05-18 07:27:43 -0500 )edit

answered 2013-06-10 17:36:20 -0500

SR gravatar image

Guanta tells the right cause.

However, personally I would discourage the use of C++ streams. Mostly because these can be quite unreadable and most important these are not thread-safe in multi-threaded enviroments. That is, your program may crash spuriously because their internal state may get corrupted.

See also and

edit flag offensive delete link more


Hehe, good point! Streams definitely have their drawbacks, guess I've advertised them in my post too much :)

Guanta gravatar imageGuanta ( 2013-06-11 05:47:55 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-05-18 05:49:02 -0500

Seen: 1,034 times

Last updated: Jun 10 '13