C-style file input/output ---unformatted input/output ---(std::getchar, std::putchar, std::ungetc)

The CI/O subset of the C++ standard library implements C-style stream input/output operations. The <cstdio> header file provides general file support and provides functions with narrow and multibyte character input/output capabilities, while the <cwchar> header file provides functions with wide character input/output capabilities.

Unformatted input/output

Read characters from stdin

std::getchar

int getchar();

Read the next character from stdin.

Equivalent to std::getc(stdin).

parameter

(none)

return value

The character obtained on success, and EOF on failure.

If an end-of-file condition causes failure, the end-of-file indicator on stdin is additionally set (see feof()). If some other error caused the failure, the error indicator on stdin is set (see ferror()).


Write characters to stdout

std::putchar

int putchar( int ch );

Write characters chto stdout. Internally, characters are converted to unsigned char just before writing.

Equivalent to putc(ch, stdout).

parameter

ch - characters to write

return value

On success, returns the character written.

On failure, returns EOF and sets the error indicator on stdout (see ferror()).

Call example

#include <cstdio>

int main(void)
{
    while (true)
    {
        char c = std::getchar();
        std::putchar(c);
    }
    return 0;
}

output

Write string to stdout

int puts( const char *str );

Writes each character from the null-terminated string strplus the appended newline character '\n' to the output stream stdout, as if written in repeated executions of std::putc.

strThe null terminating character from is not written .

parameter

str - string to write

return value

Returns a non-negative value on success

On failure, returns EOF and sets stdoutthe error indicator (see std::ferror()).

Notice

std::putsThe function appends a newline character to the output, while std::fputs does not do this.

Different implementations return different nonnegative numbers: some return the last character written, some return the number of characters written (or return it if the string is longer than INT_MAX), and some simply return a nonnegative constant.

A typical cause of failure when redirecting stdout to a file std::putsis running out of file system space.

Call example

#include <cstdio>

int main()
{
    int rc = std::puts("Hello World");

    if (rc == EOF)
    {
        std::perror("puts()");    // POSIX 要求设置 errno
    }
    return 0;
}

output

put characters back into file stream

std::ungetc

int ungetc( int ch, std::FILE *stream );

If is not equal to EOF, the character (translated to unsigned char) chis pushed into the input buffer associated with the stream in such a way that subsequent read operations from will obtain the character. External devices associated with the stream are not modified.chstreamstream

The effects of stream reseek operations std::fseek, std::fsetpos and std::rewind are discarded ungetc.

If called ungetcmore than once without intermediate reads or retrieval bits, it may fail (in other words, a playback buffer of size 1 is guaranteed, but any larger buffer is implementation-defined). If successful multiple times ungetc, the read operation ungetcobtains the played back characters in the reverse order of .

If chis equal to EOF, the operation fails without affecting the stream.

A ungetcsuccessful call to clears the end-of-file status flag std::feof.

A successful call to on a binary stream ungetcdecrements the stream position indicator by one (the behavior is undefined if the stream position indicator is zero).

A successful call to on a text stream ungetcmodifies the stream position indicator in an unspecified way, but guarantees that after all playback characters have been obtained with a read operation, the stream position indicator is equal to its ungetcvalue before .

parameter

ch - Characters to push into the input stream buffer
stream - The file stream to which characters should be played back

return value

Returned on success ch.

Returns EOF on failure, leaving the given stream unchanged.

Notice

In practice, the playback buffer size varies between 4k (Linux, MacOS) and 4 (Solaris) or the guaranteed minimum of 1 (HPUX, AIX).

The apparent size of the replay buffer can be larger if the character played back is equal to the character present at that position in the external character sequence (implementing a file position indicator that can simply decrement the read and avoid maintaining the replay buffer).

Call example

#include <cctype>
#include <cstdio>

void demo_scanf(const char* fmt, std::FILE* s)
{
    if (*fmt == '%')
    {
        switch (*++fmt)
        {
        case 'u':
        {
            int c;
            while (std::isspace(c = std::getc(s))) {} // 跳过前导空白
            unsigned int num = 0;
            while (std::isdigit(c))
            {
                num = num * 10 + c - '0';
                c = std::getc(s);
            }
            std::printf("%%u scanned %u\n", num);
            std::ungetc(c, s); // 重处理非数位
        }
        case 'c':
        {
            int c = std::getc(s);
            std::printf("%%c scanned '%c'\n", c);
        }
        }
    }
}

int main()
{
    std::FILE* file = std::fopen("input.txt", "w+");
    std::fputs("123x", file);
    std::rewind(file);
    demo_scanf("%u%c", file);
    std::fclose(file);
    return 0;
}

output

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/132790982
Recommended