First time here? Check out the FAQ!

Ask Your Question
1

printf output is too late

asked May 18 '13

andrempunkt gravatar image

updated May 18 '13

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);
    printf("HelloWorld...");
    cvWaitKey(0);
    cvReleaseImage(&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

Preview: (hide)

2 answers

Sort by » oldest newest most voted
5

answered May 18 '13

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)!

Preview: (hide)

Comments

thanks for the fast answer, worked perfect :)

andrempunkt gravatar imageandrempunkt (May 18 '13)edit
2

answered Jun 10 '13

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 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Streams#Streams and http://stackoverflow.com/questions/5328873/c-streams-vs-c-style-io

Preview: (hide)

Comments

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

Guanta gravatar imageGuanta (Jun 11 '13)edit

Question Tools

1 follower

Stats

Asked: May 18 '13

Seen: 1,670 times

Last updated: Jun 10 '13