C-style file input/output---error handling---(std::clearerr, std::feof, std::ferror, std::perror)

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.

Error handling

clear errors

std::clearerr

void clearerr( std::FILE* stream );

Resets error flags and indicators for the given file stream EOF.

parameter

stream - File stream to reset error flags

return value

(none)

Call example

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int ch = 0;
    FILE* fp = fopen("test.txt", "w");

    if (fp)
    {
        ch = std::getc(fp);
        std::printf("%c", ch);

        if (std::ferror(fp))
        {
            std::cout << "Error set" << std::endl;
            std::clearerr(fp);
        }
    }

    if (!std::ferror(fp))
    {
        std::cout << "Error reset" << std::endl;
    }

    std::fclose(fp);
    return 0;
}

output

Check end of file

std::feof

int feof( std::FILE* stream );

Checks whether the end of the given file stream has been reached.

parameter

stream - file stream to check

return value

Non-zero if the end of the file stream has been reached, 0 otherwise.

Notice

This function only reports the stream status reported by the most recent I/O operation; it does not examine the associated data source. For example, if the most recent I/O was std::fgetc returning the last byte of the file, std::feofzero is returned. The next std::fgetc fails and changes the file stream to end-of-file . Only after that std::feofdoes it return non-zero.

In typical usage, the input stream stops processing on any error; then use feofstd::ferror and std::ferror to distinguish different error conditions.

Call example

#include <cstdio>
#include <cstdlib>

int main()
{
    FILE* fp = std::fopen("test.txt", "r");
    if (!fp)
    {
        std::perror("File opening failed");
        return EXIT_FAILURE;
    }

    int c; // 注意:是 int 而非 char ,要求处理 EOF
    while ((c = std::fgetc(fp)) != EOF)   // 标准 C I/O 文件读取循环
    {
        std::putchar(c);
    }

    if (std::ferror(fp))
    {
        std::puts("I/O error when reading");
    }
    else if (std::feof(fp))
    {
        std::puts("End of file reached successfully");
    }

    std::fclose(fp);
    return 0;
}

 output

Check file for errors

std::ferror

int ferror( std::FILE* stream );

Check the given stream for errors.

parameter

stream - file stream to check

return value

Non-zero if the file stream has encountered an error, 0 otherwise.

Call example

#include <cstdio>
#include <cstdlib>
#include <clocale>
#include <cwchar>
#include <iostream>

int main(void)
{
    const char *fname = std::tmpnam(nullptr);
    std::cout << "fname:    " <<  fname << std::endl;
    std::FILE* f = std::fopen(fname, "wb");
    std::fputs("\xff\xff\n", f); // 不是合法的 UTF-8 字符序列
    std::fclose(f);

    std::setlocale(LC_ALL, "en_US.utf8");
    f = std::fopen(fname, "rb");
    std::wint_t ch;
    while ((ch = std::fgetc(f)) != WEOF) // 试图作为 UTF-8 读取
    {
        std::printf("%#x ", ch);
    }

    if (std::feof(f))
    {
        puts("EOF indicator set");
    }

    if (std::ferror(f))
    {
        puts("Error indicator set");
    }

    return 0;
}

output

Display the string corresponding to the current error on stderr

std::perror

void perror( const char *s );

Print the error code currently stored in the system variable errno to stderr.

The description is constructed by connecting the following components:

  • sThe contents of the null-terminated byte string pointed to are followed by ": " (unless sis a null pointer or sthe character pointed to by is a null character)
  • An implementation-defined errnoerror message string describing the error code stored in followed by '\n'. The error message string is equivalent to the result of std::strerror(errno).

parameter

s - Pointer to a null-terminated string holding an explanatory message

return value

(none)

Call example

#include <cmath>
#include <cerrno>
#include <cstdio>

int main()
{
    double not_a_number = std::log(-1.0);
    if (errno == EDOM)
    {
        std::perror("log(-1) failed");
    }
    return 0;
}

output

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/132947978