关于ungetc函数的问题

小小白白 2012-11-16 07:32:54
我假设ch[10]={'a','b','c','d','e','f','g','h','i','j'};,然后用
for(i=0;i<10;i++)
{
putchar(ch[i]);
ungetc(ch[i],stdin);
putchar(getchar());
putchar('\n');
}
的出来的结果是:aa
bb
cc
.....
但是用 for(i=0;i<10;i++)
{
putchar(ch[i]);
ungetc(ch[i],stdin);
}
putchar('\n');
for(i=0;i<10;i++)
{
putchar(getchar());
}
的出来的记过却是:abcdefghij
a
这是怎么回事啊,一直搞不明白ungetc具体是怎么工作的?
...全文
191 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
图灵狗 2012-11-16
  • 打赏
  • 举报
回复
NAME fgetc, fgets, getc, getchar, gets, ungetc - input of characters and strings SYNOPSIS #include <stdio.h> int fgetc(FILE *stream); char *fgets(char *s, int size, FILE *stream); int getc(FILE *stream); int getchar(void); char *gets(char *s); int ungetc(int c, FILE *stream); DESCRIPTION fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error. getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once. getchar() is equivalent to getc(stdin). gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte ('\0'). No check for buffer overrun is performed (see BUGS below). fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer. ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read operations. Pushed-back characters will be returned in reverse order; only one pushback is guaranteed. Calls to the functions described here can be mixed with each other and with calls to other input functions from the stdio library for the same input stream. For nonlocking counterparts, see unlocked_stdio(3). RETURN VALUE fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error. gets() and fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read. ungetc() returns c on success, or EOF on error. CONFORMING TO C89, C99, POSIX.1-2001. LSB deprecates gets(). POSIX.1-2008 marks gets() obsolescent. BUGS Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. It is not advisable to mix calls to input functions from the stdio library with low-level calls to read(2) for the file descriptor associ‐ ated with the input stream; the results will be undefined and very probably not what you want. SEE ALSO read(2), write(2), ferror(3), fgetwc(3), fgetws(3), fopen(3), fread(3), fseek(3), getline(3), getwchar(3), puts(3), scanf(3), ungetwc(3), unlocked_stdio(3) COLOPHON This page is part of release 3.35 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://man7.org/linux/man-pages/.
JiMoKuangXiangQu 2012-11-16
  • 打赏
  • 举报
回复
Pushes a character back onto the stream. int ungetc( int c, FILE *stream ); wint_t ungetwc( wint_t c, FILE *stream ); Parameters c Character to be pushed. stream Pointer to FILE structure. Libraries All versions of the C run-time libraries. Return Values If successful, each of these functions returns the character argument c. If c cannot be pushed back or if no character has been read, the input stream is unchanged and ungetc returns EOF; ungetwc returns WEOF. Remarks The ungetc function pushes the character c back onto stream and clears the end-of-file indicator. The stream must be open for reading. A subsequent read operation on stream starts with c. An attempt to push EOF onto the stream using ungetc is ignored. Characters placed on the stream by ungetc may be erased if fflush, fseek, or fsetpos is called before the character is read from the stream. The file-position indicator will have the value it had before the characters were pushed back. The external storage corresponding to the stream is unchanged. On a successful ungetc call against a text stream, the file-position indicator is unspecified until all the pushed-back characters are read or discarded. On each successful ungetc call against a binary stream, the file-position indicator is decremented; if its value was 0 before a call, the value is undefined after the call. Results are unpredictable if ungetc is called twice without a read or file-positioning operation between the two calls. After a call to fscanf, a call to ungetc may fail unless another read operation has been performed. This is because fscanf itself calls ungetc. ungetwc is a wide-character version of ungetc. However, on each successful ungetwc call against a text or binary stream, the value of the file-position indicator is unspecified until all pushed-back characters are read or discarded. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE Defined _ungettc ungetwc For more information about TCHAR.H routines, see Generic Text Mappings. Example /* UNGETC.C: This program first converts a character * representation of an unsigned integer to an integer. If * the program encounters a character that is not a digit, * the program uses ungetc to replace it in the stream. */ #include <stdio.h> #include <ctype.h> void main( void ) { int ch; int result = 0; printf( "Enter an integer: " ); /* Read in and convert number: */ while( ((ch = getchar()) != EOF) && isdigit( ch ) ) result = result * 10 + ch - '0'; /* Use digit. */ if( ch != EOF ) ungetc( ch, stdin ); /* Put nondigit back. */ printf( "Number = %d\nNextcharacter in stream = '%c'", result, getchar() ); } Output Enter an integer: 521a Number = 521 Nextcharacter in stream = 'a' Requirements OS Versions: Windows CE 2.0 and later. Header: ctype.h, stdlib.h, stdio.h. Link Library: coredll.dll. 不懂的函数,查查msdn就好了,不用查CSDN.

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧