Ask Your Question
1

printf output is too late

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

andrempunkt gravatar image

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

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

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

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

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

Comments

thanks for the fast answer, worked perfect :)

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

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

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

edit flag offensive delete link more

Comments

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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,638 times

Last updated: Jun 10 '13