user-0 提问: 额，C语言scanf函数缓冲区的问题

回复:
user: user-1
content: while(scanf("%d-%d-%d",&yy,&mm,&dd)!=3);

user: user-1
content: while(scanf("%d-%d-%d",&yy,&mm,&dd)!=3);  顺便鄙视zhao4zhong1 无用功

user: user-2
content: Read formatted data from the standard input stream. These are versions of scanf, _scanf_l, wscanf, _wscanf_l with security enhancements as described in Security Enhancements in the CRT.    int scanf_s(    const char *format [,       argument]...  ); int _scanf_s_l(    const char *format,    locale_t locale [,       argument]...  ); int wscanf_s(    const wchar_t *format [,       argument]...  ); int _wscanf_s_l(    const wchar_t *format,    locale_t locale [,       argument]...  );    Parameters format Format control string.  argument Optional arguments.  locale The locale to use.  Return Value Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character. If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, scanf_s and wscanf_s return EOF and set errno to EINVAL.  For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.  Remarks The scanf_s function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. If copying takes place between strings that overlap, the behavior is undefined.  wscanf_s is a wide-character version of scanf_s; the format argument to wscanf_s is a wide-character string. wscanf_s and scanf_s behave identically identically if the stream is opened in ANSI mode. scanf_s doesn't currently support input from a UNICODE stream.  The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.  Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows:  char s[10];  scanf("%9s", s, 10);  The buffer size includes the terminating null. A width specification field may be used to ensure that the token read in will fit into the buffer. If no width specification field is used, and the token read is too big to fit in the buffer, nothing will be written to that buffer.  Note:  The size parameter is of type unsigned, not size_t.    In the case of characters, one may read a single character as follows:  char c;  scanf("%c", &c, 1);  When reading multiple characters for non-null terminated strings, integers are used as the width specification and the buffer size.  char c[4];  scanf("%4c", &c, 4); // not null terminated  For more information, see scanf Width Specification.  TCHAR.H routine   _UNICODE & _MBCS not defined  _MBCS defined  _UNICODE defined   _tscanf_s  scanf_s  scanf_s  wscanf_s   _tscanf_s_l  _scanf_s_l  _scanf_s_l  _wscanf_s_l    For more information, see Format Specification Fields — scanf functions and wscanf Functions.   Requirements Routine  Required header   scanf_s, _scanf_s_l  <stdio.h>   wscanf_s, _wscanf_s_l  <stdio.h> or <wchar.h>    For additional compatibility information, see Compatibility in the Introduction.  Example   Copy Code  // crt_scanf_s.c // This program uses the scanf_s and wscanf_s functions // to read formatted input.    #include <stdio.h>  int main( void ) {    int      i,             result;    float    fp;    char     c,             s[81];    wchar_t  wc,             ws[81];     result = scanf_s( "%d %f %c %C %s %S", &i, &fp, &c, 1,                      &wc, 1, s, 80, ws, 80 );    printf( "The number of fields input is %d\n", result );    printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c,            wc, s, ws);    result = wscanf_s( L"%d %f %hc %lc %S %ls", &i, &fp, &c, 2,                       &wc, 1, s, 80, ws, 80 );    wprintf( L"The number of fields input is %d\n", result );    wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp,             c, wc, s, ws); }     Copy Code  71 98.6 h z Byte characters 36 92.3 y n Wide characters     Copy Code  The number of fields input is 6 The contents are: 71 98.599998 h z Byte characters The number of fields input is 6 The contents are: 36 92.300003 y n Wide characters    .NET Framework Equivalent System::Console::Read  System::Console::ReadLine  See also Parse methods, such as System::Double::Parse.  See Also Concepts Floating-Point Support Stream I/O Locale fscanf, _fscanf_l, fwscanf, _fwscanf_l printf, _printf_l, wprintf, _wprintf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l sscanf, _sscanf_l, swscanf, _swscanf_l

user: user-3
content: 引用 6 楼 goldfishrui 的回复:#include<stdio.h>  int main()  {  char c;  printf("Input for c:\n");  scanf_s("%c",&c);  printf("c=%c\n",c);  } 编译之后，结果就是"c=  ";无法给字符C赋值。  请大侠帮忙解答一下。  我这里可以正常运行  可以看一下你的输入是否符合要求，记得是字符型的不是字符串型的

user: user-4
content: #include<stdio.h>  int main()  {  char c;  printf("Input for c:\n");  scanf_s("%c",&c);  printf("c=%c\n",c);  } 编译之后，结果就是"c=  ";无法给字符C赋值。  请大侠帮忙解答一下。

user: user-5

user: user-6
content: 引用 2 楼 wh653 的回复:[quote=引用 1 楼 zhao4zhong1 的回复:] 在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 #include <stdio.h> #include <string.h> char s[80]; int i; char c; int main() {     while (1) {         printf("Input a integer:");fflush(stdout);         rewind(stdin);         fgets(s,80,stdin);         if (strlen(s)>10) {             printf("Input too long(>9)!\n");             continue;         }         if (2==sscanf(s,"%d%c",&i,&c)) {             if ('\n'==c) {                 printf("%d is a integer.\n",i);                 break;             }         }         if ('\n'==s[strlen(s)-1]) s[strlen(s)-1]=0;         printf("\"%s\" is NOT a integer!\n",s);     }     return 0; }      并没用。。。我想要，输入了不符合%d-%d-%d格式的，就再次等待输入，直到输入了%d-%d-%d格式的为止。 然后进行yy,mm,dd的处理，但是，，输个错的格式，第二次就算输入对的格式，还是等待输入的。。不知道怎么办[/quote] 检查scanf("%d-%d-%d")的返回值，不是3的话用一个gets吃掉然后返回去重新scanf

user: user-2
content: #include <stdio.h>  int main() {     int yy,mm,dd;     int flag=0;     int day=0;     int r;     while (1) {         printf("请输入一个日期，比如2012-1-1:");fflush(stdout);         rewind(stdin);         r=scanf("%d-%d-%d",&yy,&mm,&dd);         if (r==3) {             //判断闰年             if(yy%400==0 || (yy%4==0 && yy%100!=0))             {                 flag++;             }              switch(mm-1)             {                 case 11:{day+=30;}                 case 10:{day+=31;}                 case 9:{day+=30;}                 case 8:{day+=31;}                 case 7:{day+=31;}                 case 6:{day+=30;}                 case 5:{day+=31;}                 case 4:{day+=30;}                 case 3:{day+=31;}                 case 2:{day==day+28+flag;}                 case 1:{day+=31;}                 case 0:{day+=0;}             }              day+=dd;             printf("这天是这年的%d天\n",day);             break;         }     }     return 0; }

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 #include <stdio.h> #include <string.h> char s[80]; int i; char c; int main() {     while (1) {         printf("Input a integer:");fflush(stdout);         rewind(stdin);         fgets(s,80,stdin);         if (strlen(s)>10) {             printf("Input too long(>9)!\n");             continue;         }         if (2==sscanf(s,"%d%c",&i,&c)) {             if ('\n'==c) {                 printf("%d is a integer.\n",i);                 break;             }         }         if ('\n'==s[strlen(s)-1]) s[strlen(s)-1]=0;         printf("\"%s\" is NOT a integer!\n",s);     }     return 0; }      并没用。。。我想要，输入了不符合%d-%d-%d格式的，就再次等待输入，直到输入了%d-%d-%d格式的为止。 然后进行yy,mm,dd的处理，但是，，输个错的格式，第二次就算输入对的格式，还是等待输入的。。不知道怎么办

user: user-2
content: 在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 #include <stdio.h> #include <string.h> char s[80]; int i; char c; int main() {     while (1) {         printf("Input a integer:");fflush(stdout);         rewind(stdin);         fgets(s,80,stdin);         if (strlen(s)>10) {             printf("Input too long(>9)!\n");             continue;         }         if (2==sscanf(s,"%d%c",&i,&c)) {             if ('\n'==c) {                 printf("%d is a integer.\n",i);                 break;             }         }         if ('\n'==s[strlen(s)-1]) s[strlen(s)-1]=0;         printf("\"%s\" is NOT a integer!\n",s);     }     return 0; }