错误:stat:C编程中没有这样的文件或目录,opendir()和stat()
嘿,谢谢你的阅读 我正在制作一个程序,该程序采用1个参数(目录),读取opendir()/readdir()使用的目录中的所有文件,并使用stat显示文件类型(reg、link、directory等)。当我在shell中执行我的程序(我使用的是redhat linux)时,我收到错误“没有这样的文件或目录”。这是我的密码:错误:stat:C编程中没有这样的文件或目录,opendir()和stat(),c,linux,directory,stat,C,Linux,Directory,Stat,嘿,谢谢你的阅读 我正在制作一个程序,该程序采用1个参数(目录),读取opendir()/readdir()使用的目录中的所有文件,并使用stat显示文件类型(reg、link、directory等)。当我在shell中执行我的程序(我使用的是redhat linux)时,我收到错误“没有这样的文件或目录”。这是我的密码: #define _BSD_SOURCE #include <stdio.h> #include <dirent.h> #include <sys
#define _BSD_SOURCE
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
DIR *dirp;
struct dirent* dent;
struct stat info;
//If no args
if(argc == 1){
argv[1] = ".";
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{
//////////////////////////////////////////////////////////////////////////
if (stat(dent->d_name, &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
//////////////////////////////////////////////////////////////////////////
printf("%s \n", dent->d_name);
}
} while (dent);
closedir(dirp);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//If specified directory
if(argc > 1){
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{
/////////////////////////////////////////////////////////////////////////////////////
if (stat(dent->d_name, &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
//////////////////////////////////////////////////////////////////////////////////////
// printf("%s\n", argv[1]);
printf("%s \n", dent->d_name);
}
} while (dent);
closedir(dirp);
}
return 0;
}
\define\u BSD\u源
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
DIR*dirp;
结构方向*凹痕;
结构统计信息;
//如果没有args
如果(argc==1){
argv[1]=”;
dirp=opendir(argv[1]);//在此处指定目录:“.”是“当前目录”
做{
dent=readdir(dirp);
如果(凹痕)
{
//////////////////////////////////////////////////////////////////////////
如果(统计数据(凹痕->d_名称和信息)=-1){
佩罗(“stat”);
退出(退出失败);
}
开关(信息st\U模式和S\U IFMT){
案例S_IFBLK:printf(“块设备”);中断;
案例S_IFCHR:printf(“字符设备”);中断;
案例S_IFDIR:printf(“dir”);break;
案例S_IFFO:printf(“FIFO/管道”);中断;
案件编号:printf(“lnk”);中断;
案例S_IFREG:printf(“reg”);break;
案例S_IFSOCK:printf(“套接字”);中断;
默认值:printf(“未知?\n”);中断;
}
//////////////////////////////////////////////////////////////////////////
printf(“%s\n”,dent->d_name);
}
}而(凹痕);
closedir(dirp);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//如果指定了目录
如果(argc>1){
dirp=opendir(argv[1]);//在此处指定目录:“.”是“当前目录”
做{
dent=readdir(dirp);
如果(凹痕)
{
/////////////////////////////////////////////////////////////////////////////////////
如果(统计数据(凹痕->d_名称和信息)=-1){
佩罗(“stat”);
退出(退出失败);
}
开关(信息st\U模式和S\U IFMT){
案例S_IFBLK:printf(“块设备”);中断;
案例S_IFCHR:printf(“字符设备”);中断;
案例S_IFDIR:printf(“dir”);break;
案例S_IFFO:printf(“FIFO/管道”);中断;
案件编号:printf(“lnk”);中断;
案例S_IFREG:printf(“reg”);break;
案例S_IFSOCK:printf(“套接字”);中断;
默认值:printf(“未知?\n”);中断;
}
//////////////////////////////////////////////////////////////////////////////////////
//printf(“%s\n”,argv[1]);
printf(“%s\n”,dent->d_name);
}
}而(凹痕);
closedir(dirp);
}
返回0;
}
有什么想法吗?我有点卡住了。
谢谢你的意见
另外,“Link”类型的文件是使用stat输出的,还是必须使用lstat?不确定在这种情况下如何使用lstat,如果我将结构类型更改为“struct lstat info”,则会抛出错误。
dent->d_name
是相对于当前目录的文件名(例如“/home/barney/myfile.txt”),而不是文件的绝对完整路径(例如/home/barney/sources/myfile.txt),这是统计局所期望的
这就是stat无法找到路径的原因。在每次调用stat之前打印dent->d_name
,以观察那些不正确的路径
编辑:
您可以尝试chdir()将您的工作目录更改为argv[1]您可以演示如何调用您的程序吗?可以像“/a.out”或简单地“/a.out”这样,不带任何参数来执行当前目录哦,我明白了,这是有道理的,您建议我如何更正它?我在if语句中将dent->d_name更改为argv[1],现在我没有收到这个错误,虽然hehei在带有dent->d_name的if语句之前尝试了chdir(argv[1]),但不确定输出是否正确,这给了我错误。我对Stat和openDir有点不屑一顾,我做错了什么?我真是太傻了,我忘记了#include,这就是为什么我在chdir()上得到了隐式声明错误。。。。哈哈哈哇脸掌