- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在看一段 C 代码,它是
void printerror(char *message)
{
printf ("There was an error: '%s'\n", message);
switch (errno) {
... do stuff depending on errno
}
}
我认为这可能是个问题,因为 printf 可能会在进入函数和到达 switch
之间更改 errno。但是,printf 的联机帮助页没有说明有关设置 errno 的任何内容,所以我可以假设它永远不会设置它吗?标准中是否有任何内容可以保证哪些函数将使用和不使用 errno?
最佳答案
任何函数都可以设置 errno,但前提是将其设置为非零值。 ANSI C 规范指出:
The value of errno is zero at program startup, but is never set to zero by any library function.* The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in the Standard.
*Thus, a program that uses errno for error checking should set it to zero before a library function call, then inspect it before a subsequent library function call.
因此,如果您使用 errno,最佳实践方法是在可能失败的库调用之前立即将该值设置为 0,并在之后立即读取它。在上述情况下,添加如下内容就足够了:
int localErrno = errno
紧接在 printf 语句之前并使用 localErrno 作为您的开关。当然,这假设在失败的函数和您对 printerror 的调用之间没有库调用。如果有,您需要在调用失败后存储 errno 并将其传递给您的 printerror 函数。
关于我可以假设 C stdlib 函数不使用 errno 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4391026/
我删除数据库失败: mysql> 删除数据库 mydb; 错误 1010 (HY000):删除数据库时出错(不能 rmdir './mydb',errno: 39) 目录 db/mydb 存在于 my
据我所知,python的异常返回的errnos大部分与“linux/errno.h”中的errnos相同。但有时,事情并不像我期望的那样: 例如,当一个socket连接抛出一个socket.timeo
我们是否需要在调用函数之前将errno 重置为零?请参见下面的代码。现在的场景是 a_dest_path 是一个现有目录。但是当我执行代码时,它总是尝试 mkdir 但返回错误说该目录无法创建,因为它
在Red Hat Enterprise Linux Server release 5.7 上配置YUM本地源时,遇到了"[Errno 5] OSError: [Errno 2] No su
我正在尝试制作在线FPS游戏,到目前为止,它可以在我的本地网络上运行。我正在尝试做的是使其在全局范围内运作 过去,我曾尝试过使其他Python项目在全局范围内工作,但到目前为止,我还无法使其正常工作。
我希望获得有关在 Python 2.7 中使用urlopen时出现IOError: [Errno socket error] [Errno 10060]的更多信息。我正在使用我的个人 35MB/s 互
来自 linux 中 fwrite 的手册页, 描述 函数 fread() 从 stream 指向的流中读取数据的 nmemb 元素,每个 size 字节长,并将它们存储在 ptr 给定的位置。
我试图了解 glibc 如何在预处理器不替换 errno 符号的情况下初始化 errno。 我首先尝试自己基于csu/errno-loc.c实现了一个简单的版本和 csu/errno.c : myer
我看过这段代码: #if !defined(errno) extern int errno; #endif 所以我的问题是 errno 是 int 还是 macro ,因为使用 #if if 可以检查
errno.h 中的这 2 个 linux 错误有什么区别? 23 和 24 我尝试了 2 个不同的网站,但无法理解两者之间的区别。 [EMFILE] Too many open files. [EN
操作系统:Oracle Linux 6 [xxx@host /etc]$ uname -a Linux slc08yld 2.6.39-400.298.2.el5uek #1 SMP Mon Jan
什么样的库函数所面临的什么样的错误会影响errno并将其设置为非零值?在我下面的程序中,我打算使用 if(errno!=0) 作为条件来检查我使用的库函数是否正常运行,这是我发现的(见下面的代码):
我有一个 python 脚本,它创建一个 tar 文件,将文件移动到 tar 文件中,然后删除它们。 我可以毫无问题地手动运行脚本。但是当它从 cron 运行时,它失败了: IOError: [Err
我已经安装了 TensorFlow。当我下载 MNISTdataset 时,存在一个错误。谁能告诉我出了什么问题?非常感谢!错误详情如下: Python 2.7.9 (default, Apr 2
我正在尝试运行我的 python 代码,它显示以下内容: IOError: [Errno socket error] [Errno 54] Connection reset by peer 这是我的代
我正在将大量金融时间序列数据写入单个 CSV 文件。在一个实例中,我发现 to_csv 方法反复失败,但我终生无法弄清楚原因。在调用 to_csv 方法期间,一切都会挂起 10-15 分钟以上。在因错
我正在尝试从我的Python脚本打开文件recentlyUpdated.yaml。但当我尝试使用时:。我收到一个错误,内容是:。为什么?我怎样才能解决这个问题?
我正在尝试从我的Python脚本打开文件recentlyUpdated.yaml。但当我尝试使用时:。我收到一个错误,内容是:。为什么?我怎样才能解决这个问题?
我正在尝试从我的Python脚本打开文件recentlyUpdated.yaml。但当我尝试使用时:。我收到一个错误,内容是:。为什么?我怎样才能解决这个问题?
请解释以下程序中发生了什么。 我在程序的开头和结尾检查了 strerror(errno) 返回的地址,并确认它每次都返回相同的地址。然后一旦确定这一点,在第一种情况下我继续将相同的地址分配给 ptr,
我是一名优秀的程序员,十分优秀!