linux 中的 stat 系统调用返回错误
我正在使用 RHEL 4,
我使用 syscall stat 如下:-
if (stat ("file",&stat_obj)){
if (errno == ENOENT){
printf("File not found");
}else{
printf("Unexpected error occured %d ",errno);
}
}
有时我收到错误消息为“”发生意外错误 0”,
这意味着我收到错误为“0”。我检查了文件权限,没有问题,
这是什么意思?我无法理解为什么有时会发生这种情况?
有什么建议吗?
I am using RHEL 4
i am using syscall stat as follows:-
if (stat ("file",&stat_obj)){
if (errno == ENOENT){
printf("File not found");
}else{
printf("Unexpected error occured %d ",errno);
}
}
sometimes i get error message as ""Unexpected error occured 0"
That means i get error as "0" . i checked file permissions that are ok
what does that mean? I am not able to understand why sometimes this is happening ?
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你这样调用它,它会给你任何有意义的错误消息吗?
Does it give you any meaningful error message if you call it like this?
你的程序中有信号处理程序吗? 如果是这样,它可能会影响
errno
,然后确保它在输入时保存errno
并在返回之前将其恢复为原始值。还要确保您
#include
,并且自己没有声明errno
,特别是如果您的程序是多线程的。 errno 是一个每线程变量,因此如果将其声明为全局变量,则可能会得到错误的变量。 (在某些平台上,有时还需要一个特殊的编译标志,例如-D_TS_ERRNO
来表示线程安全的 errno,但 Linux 上不需要这样的标志。)Do you have a signal handler in your program? If so, and it may affect
errno
, then make sure that it saveserrno
on entry and restores it to its original value before returning.Also ensure that you
#include <errno.h>
, and are not declaringerrno
yourself, especially if your program is multithreaded.errno
is a per-thread variable so if you declare it as a global you can get the wrong one. (On some platforms you sometimes also need a special compilation flag like-D_TS_ERRNO
for thread-safe errno, but no such flag is needed on Linux.)