gtpc2m96 | C/C++ Language Support User's Guide |
This function pushes a character to input stream.
Format
#include <stdio.h> int ungetc(int c, FILE *stream);
This function pushes the character specified by the value of c converted to an unsigned char to the given input stream. The pushed back characters are returned by any subsequent read on the same stream in reverse order; that is, the last character pushed back will be returned first.
You can push back as many as 4 characters to a given input stream. You can call the ungetc function as many as four times consecutively; this will result in a total of 4 characters being pushed.
The stream must be open for reading. A subsequent read operation on the stream starts with c. You cannot push back end-of-file (EOF) on the stream using the ungetc function. A successful call to the ungetc function clears the EOF indicator for the stream.
Characters pushed back by the ungetc function, and subsequently not read in, will be erased if an fseek, fsetpos, rewind, or fflush function is called before the character is read from the stream. After all the pushed back characters are read in, the file position indicator is the same as it was before the characters were pushed back.
Each pushed back character backs up the file position by 1 byte. This affects the value returned by the ftell or fgetpos functions, the result of an fseek function using SEEK_CUR, or the result of an fflush function. For example, consider a file that contains a b c d e f g h:
After you have just read 'a', the current file position is at the start of 'b'. The following operations will all result in the file position being at the start of 'a', ready to read 'a' again.
/* 1 */ ungetc('a', fp); fflush(fp); /* flushes ungetc char and keeps position */ /* 2 */ ungetc('a', fp); pos = ftell(fp); /* points to first character */ fseek(fp, pos, SEEK_SET); /* 3 */ ungetc('a',fp); fseek(fp, 0, SEEK_CUR) /* starts at new file pos'n */ /* 4 */ ungetc('a', fp); fgetpos(fp, &fpos); /* gets position of first char */ fsetpos(fp, &fpos);
The ungetc function has the same restriction as any read operation for a read immediately following a write, or a write immediately following a read. Between a write and a subsequent ungetc function, there must be an intervening flush or reposition. Between an ungetc function and a subsequent write, there must be an intervening reposition.
Normal Return
Returns integer argument c converted to an unsigned char.
Error Return
If c cannot be pushed back, the ungetc function returns EOF.
Programming Considerations
None.
Examples
In the following example, the while statement reads decimal digits from an input data stream by using arithmetic statements to compose the numeric values of the numbers as it reads them. When a nondigit character appears before EOF, the ungetc function replaces the nondigit character in the input stream so that later input functions can process it.
#include <stdio.h> #include <ctype.h> int main(void) { FILE *stream; int ch; unsigned int result = 0; stream = fopen("myfile.dat","r+"); while ((ch = getc(stream)) != EOF && isdigit(ch)) { result = result * 10 + ch - '0'; } printf("result is %i\n",result); if (ch != EOF) { ungetc(ch,stream); /* Put the nondigit character back */ ch=getc(stream); printf("value put back was %c\n",ch); } }
Related Information
See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.