ungets

C

Public Domain

Pushes a NUL-terminated string back into an input stream; returns the number of characters ungotten

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <stdio.h>
#include <string.h>

int ungets(const char* str, FILE* stream) {
 if (!str) return 0;
 size_t len = strlen(str);
 for (int i=len-1; i>=0; i--)
  if (ungetc(str[i], stream) == EOF) return len-i-1;
 return len;
}