ungetc - C stdio.h

C examples for stdio.h:ungetc

Type

function

From


<cstdio>
<stdio.h>

Description

Unget character from stream, Undo previous getc operation.

Prototype

int ungetc ( int character, FILE * stream );

Parameters

ParameterDescription
character The character to be put back.
stream a FILE object.

Return Value

On success, the character put back is returned.

If the operation fails, EOF is returned.

Demo Code


#include <stdio.h>

int main ()/*from  w w w .j a v a 2 s  . co  m*/
{
  FILE * pFile;
  int c;
  char buffer [256];

  pFile = fopen ("main.cpp","rt");
  if (pFile==NULL) {
     perror ("Error opening file");
     return -2;
  }

  while (!feof (pFile)) {
        c=getc (pFile);

        if (c == EOF)
           break;

        if (c == '#')
           ungetc ('@',pFile);
        else
           ungetc (c,pFile);

        if (fgets (buffer,255,pFile) != NULL)
           fputs (buffer,stdout);
        else
           break;
  }
  return 0;
}

Related Tutorials