DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

"Unget String" Function

07.04.2007
| 2953 views |
  • submit to reddit
        As a complement to ungetc(), this C function pushes a string back onto an input stream, character by character.  It returns the number of characters pushed, or -1 if an error occurred.
void ungets(char* str, FILE* stream) {
 if (!str || !file) return -1;
 size_t len = strlen(str);
 for (int i=len-1; i>=0; i--) if (ungetc(str[i], stream) == EOF) return -1;
 return len;
}