stat()错误'没有此类文件或目录'当readdir()返回文件名时
我无法识别STAT丢弃的错误。以下程序在目录中读取所有文件并打印文件名:
DIR *dp;
struct dirent *dirp;
struct stat sb;
if((dp = opendir(argv[1]))==NULL)
{
perror("can't open dir");
}
while((dirp = readdir(dp))!=NULL)
{
if (stat(dirp->d_name, &sb) == -1) {
perror("stat");
}
printf("File name: %s \n",dirp->d_name);
}
示例输出:
/home/eipe
stat error: No such file or directory
File name: copyofsample
File name: a.out
File name: .
stat error: No such file or directory
File name: udpclient.c
File name: ..
stat error: No such file or directory
File name: client.c
stat error: No such file or directory
File name: ftpclient.c
以下是内容:
ls -l /home/eipe/c
-rwxr-xr-x 1 eipe egroup 7751 2011-02-24 15:18 a.out
-rw-r--r-- 1 eipe egroup 798 2011-02-24 13:50 client.c
-rw-r--r-- 1 eipe egroup 15 2011-02-24 15:34 copyofsample
-rw-r--r-- 1 eipe egroup 1795 2011-02-24 15:33 ftpclient.c
-rw-r--r-- 1 eipe egroup 929 2011-02-24 13:34 udpclient.c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dirp-> d_name
是目录中文件的名称:例如,“ udpclient.c”
。文件的全名是“/home/eipe/eipe/c/udpclient.c”
- 但是您当前的工作目录是/home/eipe
,所以Stat()
正在尝试访问“/home/eipe/udpclient.c”
,而不存在。您可以使用
chdir()
将工作目录更改为argv [1]
,也可以在每个文件名之前预先准备argv [1]
您调用stat()
。dirp->d_name
is the name of the file in the directory: for example,"udpclient.c"
. The full name of the file is thus"/home/eipe/c/udpclient.c"
- but your current working directory is/home/eipe
, sostat()
is trying to access"/home/eipe/udpclient.c"
, which doesn't exist.You can either change your working directory to
argv[1]
usingchdir()
, or you can prependargv[1]
to each filename before you callstat()
.请注意,POSIX 2008介绍及相关功能(系统调用),所有功能都以 后缀为熟悉的功能名称的后缀。它还定义获取与目录流相关的文件描述符。
*at()
函数在renameat()
)的情况下以一个(或两个)打开指向目录的文件描述符。这意味着在支持fstatat()
的系统上进行编码的另一种编码方式将是:使用
fstatat()
和相关功能允许您无需使用<<< a href =“ http://pubs.opengroup.org/onlinepubs/9699919799/functions/functions/chdir.html” rel =“ nofollow noreferrer”>chdir(chdir()不使用 ),或串联名称,如主要接受答案。对于可移植性,可能仍然建议使用串联 - 但是我能够在Mac OS X(10.10.1)和Linux(Ubuntu 14.04)上使用以下代码进行测试。
开发成一个完整的程序(
test-fstatat.c
):示例运行:
POSIX
at(
) )所有的名称结束 ,但是还有其他一些posix函数在上也有名称结束
。
相关
faccessat(faccessat()
fchmodat()
fstatat()
linkat()
mkdirat()
mkfifoat()
mknodat()
utimensat(utimensat(
)
fstat(
lstat()
shmat()
stat()
strcat()
strncat()
wcscat()
如果您打算使用文件流(
file *
)处理文件,则需要注意:fdopen()
Note that POSIX 2008 introduces
fstatat()
and related functions (system calls), all distinguished by theat
suffix to a familiar function name. It also definesdirfd()
to get the file descriptor associated with a directory stream.The
*at()
functions take one (or two in the case ofrenameat()
) open file descriptors that refer to a directory. This means that another way of coding this, on a system that supportsfstatat()
would be:Using
fstatat()
and related functions allows you to use relative pathnames without usingchdir()
(which is dangerous; it is hard to get back to where you started without usingfchdir()
), or concatenating names as shown in the main accepted answer. For portability, it is probably still advisable to use concatenation anyway — but I was able to test this on Mac OS X (10.10.1) and Linux (Ubuntu 14.04), using the code below.Developed into a complete program (
test-fstatat.c
):Sample run:
POSIX
*at()
functionsThe relevant set of functions (other than
dirfd()
) all have names endingat
, but there are some other POSIX functions that also have names endingat
.Relevant:
dirfd()
faccessat()
fchmodat()
fchownat()
fstatat()
linkat()
mkdirat()
mkfifoat()
mknodat()
openat()
readlinkat()
renameat()
symlinkat()
unlinkat()
utimensat()
Not directly relevant:
creat()
fstat()
lstat()
shmat()
stat()
strcat()
strncat()
wcscat()
wcsncat()
If you plan to process files using file streams (
FILE *
), you need to be aware of:fdopen()