Thursday, November 12, 2009

eclipse CDT console scanf 버그(windows환경)

eclipse CDT console scanf 버그(windows환경)

이클립스 CDT 콘솔 이용해서 실행시

아래와같이 scanf 함수가 있다면 아무것도 출력되지 않습니다.
먼저 입력을 한 이후 화면이 출력되는 현상을 볼수 있습니다.

----------------------------------------------------------------
#include

int main()
{
int nAge;
int nBirth;

printf("Age : ");
scanf("%d", &nAge);

printf("Age = %d\n", nAge);
return 0;
}
----------------------------------------------------------------

해결방법: printf나 출력문 이후에 버퍼를 비워줌 > fflush(stdout);
예)
----------------------------------------------------------------
#include

int main()
{
int nAge;
int nBirth;

printf("Age : ");
fflush(stdout);
scanf("%d", &nAge);

printf("Age = %d\n", nAge);
return 0;
}
----------------------------------------------------------------


버그에대한 수정답변
It is not easy to "fix" and it depends on the OS.

The line buffering policy for the stdio or for C++ iostream, is base on whether
or not the output stream maps to a real console or a pty for Linux user.

On Linux, we find a way to do this by creating a pseudo pty, this will
work for debug launch and normal launch(in most of the case).

On Windows, for the GDB backend we create a command console that is
associated with the application, this will work for debug launch.

On Windows for normal launch we have no solution, so the output will not
be line buffered. So yes this is still a problem for windows.

This bug was flip to fix because it was targetting Linux(Unix-All) and
a solution was provided.

The other comments are for windows and folks should make a new PR for it.

No comments: