C프로그래밍 파워 업그레이드 Q4-1에서 배운 fflush(stdin)의 문제점
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int total = 0;
6 char input;
7
8 while(1)
9 {
10 fputs("Data input (Ctrl+D to exit) : ", stdout);
11 input = getchar();
12 if(input==EOF)
13 break;
14
15 fflush(stdin);
16 total++;
17 }
18
19 printf("입력된 문자의 수 : %d\n", total);
20 return 0;
21 }
이것을 실행 할 시
Data input (Ctrl+D to exit) : a
Data input (Ctrl+D to exit) : Data input (Ctrl+D to exit) : b
Data input (Ctrl+D to exit) : Data input (Ctrl+D to exit) : 입력된 문자의 수 : 4
결과가 이렇게 나온다.
디버깅을 해본 결과 fflush(stdin)이 '\n'(엔터)값을 버퍼에서 지우지 못하고 있었다.
(디버깅은 리눅스 환경에서 gdb로 우선 해봤는데 변수 값 추적하는 걸 아직 못해서 visual studio 2017로 했다.)
-----
google에 C언어 fflush라고 검색을 해봤다.
아래와 같은 사이트를 찾았다.
http://mjson.tistory.com/190
감사합니다. 덕분에 많은 도움이 되었습니다.
-----
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int total = 0;
6 char input;
7
8 while(1)
9 {
10 fputs("Data input (Ctrl+D to exit) : ", stdout);
11 input = getchar();
12 if(input==EOF)
13 break;
14
15 while(getchar() != '\n')
16 total++;
17 }
18
19 printf("입력된 문자의 수 : %d\n", total);
20 return 0;
21 }
Data input (Ctrl+D to exit) : a
Data input (Ctrl+D to exit) : b
Data input (Ctrl+D to exit) : c
Data input (Ctrl+D to exit) : 입력된 문자의 수 : 3
다음과 같이 결과가 잘 나온다.
:^)
http://mjson.tistory.com/190 님 다시 한번 감사드립니다.
2
3 int main(void)
4 {
5 int total = 0;
6 char input;
7
8 while(1)
9 {
10 fputs("Data input (Ctrl+D to exit) : ", stdout);
11 input = getchar();
12 if(input==EOF)
13 break;
14
15 fflush(stdin);
16 total++;
17 }
18
19 printf("입력된 문자의 수 : %d\n", total);
20 return 0;
21 }
이것을 실행 할 시
Data input (Ctrl+D to exit) : a
Data input (Ctrl+D to exit) : Data input (Ctrl+D to exit) : b
Data input (Ctrl+D to exit) : Data input (Ctrl+D to exit) : 입력된 문자의 수 : 4
결과가 이렇게 나온다.
디버깅을 해본 결과 fflush(stdin)이 '\n'(엔터)값을 버퍼에서 지우지 못하고 있었다.
(디버깅은 리눅스 환경에서 gdb로 우선 해봤는데 변수 값 추적하는 걸 아직 못해서 visual studio 2017로 했다.)
-----
google에 C언어 fflush라고 검색을 해봤다.
아래와 같은 사이트를 찾았다.
http://mjson.tistory.com/190
감사합니다. 덕분에 많은 도움이 되었습니다.
-----
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int total = 0;
6 char input;
7
8 while(1)
9 {
10 fputs("Data input (Ctrl+D to exit) : ", stdout);
11 input = getchar();
12 if(input==EOF)
13 break;
14
15 while(getchar() != '\n')
16 total++;
17 }
18
19 printf("입력된 문자의 수 : %d\n", total);
20 return 0;
21 }
Data input (Ctrl+D to exit) : a
Data input (Ctrl+D to exit) : b
Data input (Ctrl+D to exit) : c
Data input (Ctrl+D to exit) : 입력된 문자의 수 : 3
다음과 같이 결과가 잘 나온다.
:^)
http://mjson.tistory.com/190 님 다시 한번 감사드립니다.