diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/io%2Fungetc.html | |
new repository
Diffstat (limited to 'devdocs/c/io%2Fungetc.html')
| -rw-r--r-- | devdocs/c/io%2Fungetc.html | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/devdocs/c/io%2Fungetc.html b/devdocs/c/io%2Fungetc.html new file mode 100644 index 00000000..ace91ea5 --- /dev/null +++ b/devdocs/c/io%2Fungetc.html @@ -0,0 +1,76 @@ + <h1 id="firstHeading" class="firstHeading">ungetc</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int ungetc( int ch, FILE *stream );</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>If <code>ch</code> does not equal <code><a href="../io" title="c/io">EOF</a></code>, pushes the character <code>ch</code> (reinterpreted as <code>unsigned char</code>) into the input buffer associated with the stream <code>stream</code> in such a manner that subsequent read operation from <code>stream</code> will retrieve that character. The external device associated with the stream is not modified.</p> +<p>Stream repositioning operations <code><a href="fseek" title="c/io/fseek">fseek</a></code>, <code><a href="fsetpos" title="c/io/fsetpos">fsetpos</a></code>, and <code><a href="rewind" title="c/io/rewind">rewind</a></code> discard the effects of <code>ungetc</code>.</p> +<p>If <code>ungetc</code> is called more than once without an intervening read or repositioning, it may fail (in other words, a pushback buffer of size 1 is guaranteed, but any larger buffer is implementation-defined). If multiple successful <code>ungetc</code> were performed, read operations retrieve the pushed-back characters in reverse order of <code>ungetc</code>.</p> +<p>If <code>ch</code> equals <code><a href="../io" title="c/io">EOF</a></code>, the operation fails and the stream is not affected.</p> +<p>A successful call to <code>ungetc</code> clears the end of file status flag <code><a href="feof" title="c/io/feof">feof</a></code>.</p> +<p>A successful call to <code>ungetc</code> on a binary stream decrements the stream position indicator by one (the behavior is indeterminate if the stream position indicator was zero).</p> +<p>A successful call to <code>ungetc</code> on a text stream modifies the stream position indicator in unspecified manner but guarantees that after all pushed-back characters are retrieved with a read operation, the stream position indicator is equal to its value before <code>ungetc</code>.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> ch </td> <td> - </td> <td> character to be pushed into the input stream buffer </td> +</tr> <tr class="t-par"> <td> stream </td> <td> - </td> <td> file stream to put the character back to </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>On success <code>ch</code> is returned.</p> +<p>On failure <code><a href="../io" title="c/io">EOF</a></code> is returned and the given stream remains unchanged.</p> +<h3 id="Notes"> Notes</h3> <p>The size of the pushback buffer varies in practice from 4k (Linux, MacOS) to as little as 4 (Solaris) or the guaranteed minimum 1 (HPUX, AIX).</p> +<p>The apparent size of the pushback buffer may be larger if the character that is pushed back equals the character existing at that location in the external character sequence (the implementation may simply decrement the read file position indicator and avoid maintaining a pushback buffer).</p> +<h3 id="Example"> Example</h3> <div class="t-example"> +<p>demonstrates the original purpose of <code>ungetc</code>: implementation of <code><a href="fscanf" title="c/io/fscanf">scanf</a></code></p> +<div class="c source-c"><pre data-language="c">#include <ctype.h> +#include <stdio.h> + +void demo_scanf(const char* fmt, FILE* s) +{ + while (*fmt != '\0') { + if (*fmt == '%') { + int c; + switch (*++fmt) { + case 'u': + while (isspace(c=getc(s))) {} + unsigned int num = 0; + while (isdigit(c)) { + num = num*10 + c-'0'; + c = getc(s); + } + printf("%%u scanned %u\n", num); + ungetc(c, s); + break; + case 'c': + c = getc(s); + printf("%%c scanned '%c'\n", c); + break; + } + } else { + ++fmt; + } + } +} + +int main(void) +{ + FILE* f = fopen("input.txt", "w+"); + if (f != NULL) { + fputs("123x", f); + rewind(f); + demo_scanf("%u%c", f); + fclose(f); + } + return 0; +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">%u scanned 123 +%c scanned 'x'</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.21.7.10 The ungetc function (p: 243) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.21.7.10 The ungetc function (p: 334) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.19.7.11 The ungetc function (p: 300) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.9.7.11 The ungetc function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fgetc" title="c/io/fgetc"> <span class="t-lines"><span>fgetc</span><span>getc</span></span></a></div> </td> <td> gets a character from a file stream <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/ungetc" title="cpp/io/c/ungetc">C++ documentation</a></span> for <code>ungetc</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/io/ungetc" class="_attribution-link">https://en.cppreference.com/w/c/io/ungetc</a> + </p> +</div> |
