[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


ungetc

Place a character back into a stream.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdio.h>
int ungetc(int c, FILE *stream);
Parameters:

Parameters for this facility are:

c  
int  
The character to return to a file  
stream  
FILE *  
A pointer to a FILE stream  

Remarks:

The ungetc() function places character c back into stream's buffer. The next read operation will read the character placed by ungetc(). Only one character can be pushed back into a buffer until a read operation is performed.

The function's effect is ignored when an fseek(), fsetpos(), or rewind() operation is performed.

Return:

ungetc() returns c if it is successful and returns EOF if it fails.

See Also:

"Wide Character and Byte Character Stream Orientation"

"fseek"

"fsetpos"

"rewind"

Example of ungetc() usage.:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	FILE *f;
	int c;

	// create a new file for output and input
	if ( (f = fopen("myfoo", "w+")) == NULL) {
		printf("Can't open myfoo.\n");
		exit(1);
	}

	// output text to the file
	fprintf(f, "The quick brown fox\n");
	fprintf(f, "jumped over the moon.\n");

	// move the file position indicator 
	// to the beginning of the file
	rewind(f);

	printf("Reading each character twice.\n");

	// read a character
	while ( (c = fgetc(f)) != EOF) {
		putchar(c);
	// put the character back into the stream
		ungetc(c, f);
		c = fgetc(f);// read the same character again
		putchar(c);
	}

	fclose(f);

	return 0;
}


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000