codescracker


c

C ungetc()



« Previous Function Next Function »


ungetc()

The ungetc() function returns the character specified by the low-order byte of ch to the input stream stream. This character will then be obtained by the next read operation on the stream. A call to fflush(), fseek(), or rewind() undoes an ungetc() operation and discards the character.

A one-character push back is guaranteed; however, some implementations will accept more.

A call to the ungetc() clears the end-of-file flag associated with specified stream. The value of file position indicator for a text stream is undefined until all pushed-back characters are read, in which case it will be same as it was prior to the first ungetc() call. For binary stream, each ungetc() call decrements the file position indicator.

ungetc() Syntax

#include<stdio.h>
int ungetc(int ch, FILE *stream);

The return value is equal to the ch on success, otherwise returns EOF on failure.

ungetc() Example

Following function reads words from the input stream pointed to by fp. The terminating character is returned to the stream for later use. For example, given input count/10, the first call to read_word() returns count and puts the "/" back into the input stream.

void read_word(FILE *fp, char *token)
{
	while(isalpha(*token=getc(fp)))
	token++;
	ungetc(*token, fp);
}

« Previous Function Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test