User Tools

Site Tools


c:stdio.h:ungetc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

c:stdio.h:ungetc [2013/01/22 22:02] (current)
Line 1: Line 1:
 +{{keywords>wiki library source code example reference}}
 +===== ungetc =====
 +<code c>
 +#include <stdio.h> /* including standard library */
 +//#include <windows.h> /* uncomment this for Windows */
 +
 + int ungetc(int chr, FILE *stream);
 +</code>
 +Pushes chr (which must not be EOF), onto (input) stream stream such that it will be returned by the next read. Only one character of pushback is guaranteed (for each stream). Returns chr, or EOF on error.
 +
 +
 +
 +===== C Sourcecode Example =====
 +<code c>
 +/* 
 + * ungetc example code
 + * http://code-reference.com/c/stdio.h/ungetc 
 + */
 +
 +#include <stdio.h> /* including standard library */
 +//#include <windows.h> /* uncomment this for Windows */
 +
 +
 +int main(void)
 +{
 +   FILE *stream;
 +   int ch;
 +
 +   stream = fopen("test.txt","w");
 +   fputs("Howdy, i write something into the test.txt file ",stream);
 +   
 +   if(stream == NULL) {
 +       perror("sorry but test.txt has");
 +     }
 +   
 +   freopen("test.txt","r",stream);
 +
 +   while ((ch = getc(stream)) != EOF)
 +     {
 +        putc(ch, stdout);
 +        ch = getc(stream);
 +        ungetc(ch,stream); /* comment this out to see what happen */
 +     }
 +    
 +    printf("\n");
 +    return 0;
 +}
 +
 +</code>
 +
 +==== output ====
 +    
 +== with ungetc ==
 +    user@host:~$ ./ungetc 
 +    Howdy, i write something into the test.txt file
 +
 +== without ungetc ==
 +    user@host:~$ ./ungetc 
 +    Hwy  rt oehn notets.x ie
 +
  
advertising
c/stdio.h/ungetc.txt · Last modified: 2013/01/22 22:02 (external edit)

Impressum Datenschutz