Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

"Unget string" function

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;
}
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS