fflush(stdin); <---이거 왜 gcc에서 먹통인가요???

익명 사용자의 이미지

표준입력으로 입력을 받은다음..

fflush(stdin);

으로 입력스트림 비워버리려고 하는데..
gcc에서 전혀 안먹히네요??

익명 사용자의 이미지

man 페이지를 보면 이런 내용이..

DESCRIPTION
The function fflush forces a write of all buffered data for the
given output or update stream via the
stream's underlying write function. The open status of the
stream is unaffected.

If the stream argument is NULL, fflush flushes all open output
streams.

^^; stdin은 입력받는 스트림인데, fflush 해봐야 소용없겠죠?

익명 사용자의 이미지

그럼 gcc에선 입력스트림 비우기 위해 어떤 함수 쓰나요??
출력스트림을 비우는게 있으면
입력스트림도 비우는게 있을텐데..

char junk[80];
gets(junk);
이런방법은 말고..

익명 사용자의 이미지

gcc 가 아니라 glibc이죠..^^

int tcflush (int filedes, int queue)
The tcflush function is used to clear the input and/or output queues
associated with the terminal file filedes. The queue argument specifies
which queue(s) to clear, and can be one of the following values

TCIFLUSH
Clear any input data received, but not yet read.
TCOFLUSH
Clear any output data written, but not yet transmitted.
TCIOFLUSH
Clear both queued input and output.

익명 사용자의 이미지

제가 아직 테스트는 안해 봤는데.

여기서 버퍼를 클리어 한다는게,
그냥 버린다는 것인지? 아니면 싱크를 의미하는 것인지
모르겠네요...

ps. 전 입력 버퍼에서 바로 바로 읽어 드려야
하는 상황이라면(줄단위 입력이 아니라면)
getc를 사용하거든요

익명 사용자의 이미지

죄송합니다만..

제가 프로그램공부한지 얼마 안되서 그러는데요..

표준입력 버퍼를 비워버릴려면..

tcflush(0, TCIFLUSH);

이렇게 쓰는게 맞나요?

제가 이렇게 한번 해봤는데요..

fgets(ch, 5, stdin); tcflush(0, TCIFLUSH);
fgets(ch2, 5, stdin);

이렇게 해서 실행시켜서

12345

이렇게 치면

ch 에는 1234가 저장되구 버퍼가 비워졌으면 다시

입력 받아야 되는데..

그냥 ch2에 5가 입력 되버리더군요.

0도 해보고 1도 해보고 TCIFLUSH, TCOFLUSH, TCIOFLUSH

다 해봤는데..계속 같은 현상이..

너무 초보적인 질문 드려서 죄송합니다.

꼭좀 가르쳐 주시면 감사하겠습니다.

익명 사용자의 이미지

#include
#include
#include

int main(void)
{
char ch[5];
char ch2[5];
int read_n1;
int read_n2;

read_n1 = read(0, ch,5);
ch[read_n1-1] = '\0';
tcflush(0, TCIFLUSH);
read_n2 = read(0, ch2,5);
ch2[read_n2-1] = '\0';
printf("ch%s , ch2%s",ch,ch2);
}

이렇게 하면 잘 돼는군요.^^

stdio.h 함수는 안되는것 같습니다. 내부적으로 버퍼를 이용하거든요..

그거 비우는거는..찾아볼께요..

익명 사용자의 이미지

#include
#include
#include
#include

int main(void)
{
char ch[5];
char ch2[5];
int read_n1;
int read_n2;
fgets(ch, 5, stdin);
__fpurge (stdin); // key point
fgets(ch2, 5, stdin);
printf("ch%s , ch2%s",ch,ch2);
}

이렇게 하니 되네요.

아 일해야 하는데...

저 짤리겠어요.

후후...좋은 하루.~~~

익명 사용자의 이미지

신경써 주셔서 너무 감사합니다.

^^

메일로 가면 귀찮으실까바 그냥 제글에 리플 달았습니다.

(근데 저 헤더파일이랑 함수 man 해보니깐 안나오네요...)

익명 사용자의 이미지

움 stdio_ext.h 이란 헤더가 없다고 나와서
아직 못해보고 있는데..
__fpurge() 라는 함수는 아무래도 안시표준하고는 거리가
있는 함수같은지라..

안시표준 함수로..
입력스트림에 아직 남아있는 불필요한 입력을
지우는 함수는 정녕 없나요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.