检查C中的fwrite是否成功,perror

时间:2022-09-06 18:46:55

With fwrite returning the number of successful elements written to the file, by saying:

使用fwrite返回写入文件的成功元素的数量,通过说:

if (!(fwrite(...))) {
    fprintf(stderr, "Failure");
    //perror(???)  I sometimes see code that says perror here and I don't know 
    //exactly what this does.
}

Does this check for successful writing to the file? Are there other things to worry about?

这是否检查成功写入文件?还有其他需要担心的事情吗?

Thanks.

5 个解决方案

#1


11  

In short, not quite. fwrite returns the number of elements successfully written; you need to check this against the number of elements you intended to write i.e. those you passed in argument to fwrite.

总之,不完全。 fwrite返回成功写入的元素数;你需要根据你想写的元素数来检查这一点,即你在fwrite参数中传递的元素。

What you've done checks that some elements have been written.

你所做的就是检查一些元素是否已被写入。

Here's a reference for perror.

这是perror的参考。

Interprets the value of the global variable errno into a string and prints that string to stderr (standard error output stream, usually the screen), optionaly preceding it with the custom message specified in str. errno is an integral variable whose value describes the last error produced by a call to a library function. The error strings produced by perror depend on the developing platform and compiler. If the parameter str is not a null pointer, str is printed followed by a colon (:) and a space. Then, whether str was a null pointer or not, the generated error description is printed followed by a newline character ('\n'). perror should be called right after the error was produced, otherwise it can be overwritten in calls to other functions.

将全局变量errno的值解释为字符串,并将该字符串打印到stderr(标准错误输出流,通常是屏幕),并在其前面使用str中指定的自定义消息。 errno是一个整数变量,其值描述了对库函数调用产生的最后一个错误。 perror生成的错误字符串取决于开发平台和编译器。如果参数str不是空指针,则打印str,后跟冒号(:)和空格。然后,无论str是否为空指针,都会打印生成的错误描述,后跟换行符('\ n')。应该在生成错误后立即调用perror,否则可以在调用其他函数时覆盖它。

#2


2  

Your code might not check for errors properly. Use

您的代码可能无法正确检查错误。使用

if (fwrite(ptr, size, num, f) != num) {
    // An error occurred, handle it somehow
}

#3


0  

STRERROR(3)            FreeBSD Library Functions Manual            STRERROR(3)

NAME
     perror, strerror, strerror_r, sys_errlist, sys_nerr — system error mes‐
     sages

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <stdio.h>

     void
     perror(const char *string);

     ...

DESCRIPTION
     ...

     The perror() function finds the error message corresponding to the cur‐
     rent value of the global variable errno (intro(2)) and writes it, fol‐
     lowed by a newline, to the standard error file descriptor.  If the argu‐
     ment string is non‐NULL and does not point to the null character, this
     string is prepended to the message string and separated from it by a
     colon and space (“: ”); otherwise, only the error message string is
     printed.

...

STANDARDS
     The perror() and strerror() functions conform to ISO/IEC 9899:1999
     (“ISO C99”).  ...

#4


0  

From the Linux man page of fwrite

来自fwrite的Linux手册页

fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

fread()和fwrite()返回成功读取或写入的项目数(即,不是字符数)。如果发生错误或达到文件结尾,则返回值为短项目计数(或零)。

so you need to compare with what is expected return value.

所以你需要与预期的回报值进行比较。

In many cases you may need to check for errno equal to EAGAIN or EINTR, in which case you normally want to retry the write request, while in other cases you want to handle short writes gracefully.

在许多情况下,您可能需要检查errno是否等于EAGAIN或EINTR,在这种情况下,您通常需要重试写入请求,而在其他情况下,您希望正常处理短写入。

For fwrite, on a short write (where less than your entire data was written) you can check feof() and/or ferror() to see if the stream is returning and end-of-file, EOF, such as if a PIPE was closed, or if the stream has its error inducator flag set.

对于fwrite,在短写入(少于整个数据写入)时,您可以检查feof()和/或ferror()以查看流是否返回以及文件结束,EOF,例如PIPE已关闭,或者流是否设置了错误inducator标志。

#5


0  

You can also use explain_fwrite(), explain_errno_fwrite, ... from libexplain.

您还可以使用libexplain中的explain_fwrite(),explain_errno_fwrite,....

The man page explains:

手册页解释说:

The explain_fwrite function is used to obtain an explanation of an error returned by the fwrite(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail.

explain_fwrite函数用于获取fwrite(3)系统调用返回的错误的说明。消息将包含的最少值是strerror(errno)的值,但通常它会做得更好,并更详细地指出根本原因。

The errno global variable will be used to obtain the error value to be decoded.

errno全局变量将用于获取要解码的错误值。

This function is intended to be used in a fashion similar to the following example:

此函数旨在以类似于以下示例的方式使用:

if (fwrite(ptr, size, nmemb, fp) < 0)
{
    fprintf(stderr, "%s\n", explain_fwrite(ptr, size, nmemb, fp));
    exit(EXIT_FAILURE);
}

Caveat: This method is not thread-safe.

警告:此方法不是线程安全的。

#1


11  

In short, not quite. fwrite returns the number of elements successfully written; you need to check this against the number of elements you intended to write i.e. those you passed in argument to fwrite.

总之,不完全。 fwrite返回成功写入的元素数;你需要根据你想写的元素数来检查这一点,即你在fwrite参数中传递的元素。

What you've done checks that some elements have been written.

你所做的就是检查一些元素是否已被写入。

Here's a reference for perror.

这是perror的参考。

Interprets the value of the global variable errno into a string and prints that string to stderr (standard error output stream, usually the screen), optionaly preceding it with the custom message specified in str. errno is an integral variable whose value describes the last error produced by a call to a library function. The error strings produced by perror depend on the developing platform and compiler. If the parameter str is not a null pointer, str is printed followed by a colon (:) and a space. Then, whether str was a null pointer or not, the generated error description is printed followed by a newline character ('\n'). perror should be called right after the error was produced, otherwise it can be overwritten in calls to other functions.

将全局变量errno的值解释为字符串,并将该字符串打印到stderr(标准错误输出流,通常是屏幕),并在其前面使用str中指定的自定义消息。 errno是一个整数变量,其值描述了对库函数调用产生的最后一个错误。 perror生成的错误字符串取决于开发平台和编译器。如果参数str不是空指针,则打印str,后跟冒号(:)和空格。然后,无论str是否为空指针,都会打印生成的错误描述,后跟换行符('\ n')。应该在生成错误后立即调用perror,否则可以在调用其他函数时覆盖它。

#2


2  

Your code might not check for errors properly. Use

您的代码可能无法正确检查错误。使用

if (fwrite(ptr, size, num, f) != num) {
    // An error occurred, handle it somehow
}

#3


0  

STRERROR(3)            FreeBSD Library Functions Manual            STRERROR(3)

NAME
     perror, strerror, strerror_r, sys_errlist, sys_nerr — system error mes‐
     sages

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <stdio.h>

     void
     perror(const char *string);

     ...

DESCRIPTION
     ...

     The perror() function finds the error message corresponding to the cur‐
     rent value of the global variable errno (intro(2)) and writes it, fol‐
     lowed by a newline, to the standard error file descriptor.  If the argu‐
     ment string is non‐NULL and does not point to the null character, this
     string is prepended to the message string and separated from it by a
     colon and space (“: ”); otherwise, only the error message string is
     printed.

...

STANDARDS
     The perror() and strerror() functions conform to ISO/IEC 9899:1999
     (“ISO C99”).  ...

#4


0  

From the Linux man page of fwrite

来自fwrite的Linux手册页

fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

fread()和fwrite()返回成功读取或写入的项目数(即,不是字符数)。如果发生错误或达到文件结尾,则返回值为短项目计数(或零)。

so you need to compare with what is expected return value.

所以你需要与预期的回报值进行比较。

In many cases you may need to check for errno equal to EAGAIN or EINTR, in which case you normally want to retry the write request, while in other cases you want to handle short writes gracefully.

在许多情况下,您可能需要检查errno是否等于EAGAIN或EINTR,在这种情况下,您通常需要重试写入请求,而在其他情况下,您希望正常处理短写入。

For fwrite, on a short write (where less than your entire data was written) you can check feof() and/or ferror() to see if the stream is returning and end-of-file, EOF, such as if a PIPE was closed, or if the stream has its error inducator flag set.

对于fwrite,在短写入(少于整个数据写入)时,您可以检查feof()和/或ferror()以查看流是否返回以及文件结束,EOF,例如PIPE已关闭,或者流是否设置了错误inducator标志。

#5


0  

You can also use explain_fwrite(), explain_errno_fwrite, ... from libexplain.

您还可以使用libexplain中的explain_fwrite(),explain_errno_fwrite,....

The man page explains:

手册页解释说:

The explain_fwrite function is used to obtain an explanation of an error returned by the fwrite(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail.

explain_fwrite函数用于获取fwrite(3)系统调用返回的错误的说明。消息将包含的最少值是strerror(errno)的值,但通常它会做得更好,并更详细地指出根本原因。

The errno global variable will be used to obtain the error value to be decoded.

errno全局变量将用于获取要解码的错误值。

This function is intended to be used in a fashion similar to the following example:

此函数旨在以类似于以下示例的方式使用:

if (fwrite(ptr, size, nmemb, fp) < 0)
{
    fprintf(stderr, "%s\n", explain_fwrite(ptr, size, nmemb, fp));
    exit(EXIT_FAILURE);
}

Caveat: This method is not thread-safe.

警告:此方法不是线程安全的。