C 检查路径是文件还是目录
我刚开始学习C,正在尝试做路径列表。我尝试使用C 检查路径是文件还是目录,c,C,我刚开始学习C,正在尝试做路径列表。我尝试使用dirent列出目录中的路径,并尝试使用stat检查结果是否为文件或目录。但是,即使路径是文件,它也会将每个路径作为目录返回 这是我的代码: [编辑] #包括 #包括 #包括 #包括 内部主(空) { DIR*mydir=opendir(“/Library/Logs”); 字符路径[250]; struct dirent*entry=NULL; 结构统计buf; 而((entry=readdir(mydir)))/*如果我们得到EOF,表达式是0和
dirent
列出目录中的路径,并尝试使用stat
检查结果是否为文件或目录。但是,即使路径是文件,它也会将每个路径作为目录返回
这是我的代码:[编辑]
#包括
#包括
#包括
#包括
内部主(空)
{
DIR*mydir=opendir(“/Library/Logs”);
字符路径[250];
struct dirent*entry=NULL;
结构统计buf;
而((entry=readdir(mydir)))/*如果我们得到EOF,表达式是0和
*循环停止*/
{
snprintf(路径,250,“%s”,条目->数据单元名称);
stat(路径和buf);
if(S_ISDIR(buf.st_模式))
printf(“D:%s\n”,路径);
否则如果(S_ISREG(buf.st_模式))
printf(“F:%s\n”,路径);
其他的
printf(“O:%s\n”,路径);
}
}
此时,您对正在查找的目录没有任何上下文
在调用stat
(使用strcat
或snprintf
)之前,您需要创建一个缓冲区,并连接目录
/文件名
)
检查对
stat
的返回值的调用——如果非零,请查看errno
以查看出了什么问题。我猜它目前正在报告一个eNONT。试试这个,使用readdir和dirent
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
/* "readdir" etc. are defined here. */
#include <dirent.h>
/* limits.h defines "PATH_MAX". */
#include <limits.h>
/* List the files in "dir_name". */
static void
list_dir (const char * dir_name)
{
DIR * d;
/* Open the directory specified by "dir_name". */
d = opendir (dir_name);
/* Check it was opened. */
if (! d) {
fprintf (stderr, "Cannot open directory '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
while (1) {
struct dirent * entry;
const char * d_name;
/* "Readdir" gets subsequent entries from "d". */
entry = readdir (d);
if (! entry) {
/* There are no more entries in this directory, so break
out of the while loop. */
break;
}
d_name = entry->d_name;
/* Print the name of the file and directory. */
printf ("%s/%s\n", dir_name, d_name);
/* See if "entry" is a subdirectory of "d". */
if (entry->d_type & DT_DIR) {
/* Check that the directory is not "d" or d's parent. */
if (strcmp (d_name, "..") != 0 &&
strcmp (d_name, ".") != 0) {
int path_length;
char path[PATH_MAX];
path_length = snprintf (path, PATH_MAX,
"%s/%s", dir_name, d_name);
printf ("%s\n", path);
if (path_length >= PATH_MAX) {
fprintf (stderr, "Path length has got too long.\n");
exit (EXIT_FAILURE);
}
/* Recursively call "list_dir" with the new path. */
list_dir (path);
}
}
}
/* After going through all the entries, close the directory. */
if (closedir (d)) {
fprintf (stderr, "Could not close '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
}
int main ()
{
list_dir ("/Library/Logs");
return 0;
}
#包括
#包括
#包括
#包括
#包括
/*此处定义了“readdir”等*/
#包括
/*h定义了“路径_MAX”*/
#包括
/*在“目录名称”中列出文件*/
静态空隙
列表目录(常量字符*目录名称)
{
DIR*d;
/*打开“dir_name”指定的目录*/
d=opendir(目录名称);
/*检查它是否已打开*/
如果(!d){
fprintf(stderr,“无法打开目录'%s':%s\n”,
董事姓名,strerror(errno));
退出(退出失败);
}
而(1){
结构方向*条目;
常量字符*d_名称;
/*“Readdir”从“d”获取后续条目*/
条目=readdir(d);
如果(!输入){
/*此目录中没有更多条目,请中断
在while循环之外*/
打破
}
d_name=条目->d_name;
/*打印文件和目录的名称*/
printf(“%s/%s\n”,目录名,数据名);
/*查看“entry”是否是“d”的子目录*/
如果(输入->数据类型和数据目录){
/*检查目录是否不是“d”或d的父目录*/
如果(strcmp(d_name,“…”)!=0&&
strcmp(数据单元名称“.”!=0){
int路径长度;
字符路径[path_MAX];
路径长度=snprintf(路径,路径最大值,
“%s/%s”,目录名,数据名);
printf(“%s\n”,路径);
如果(路径长度>=路径最大值){
fprintf(stderr,“路径长度太长。\n”);
退出(退出失败);
}
/*使用新路径递归调用“list_dir”*/
列表目录(路径);
}
}
}
/*检查完所有条目后,关闭目录*/
if(closedir(d)){
fprintf(stderr,“无法关闭“%s”:%s\n”,
董事姓名,strerror(errno));
退出(退出失败);
}
}
int main()
{
列表目录(“/Library/Logs”);
返回0;
}
输出将是
/Library/Logs/.<br />
/Library/Logs/..<br />
/Library/Logs/somedir001<br />
/Library/Logs/somedir002
/Library/Logs/
/库/日志/。
/库/Logs/somedir001
/库/日志/somedir002
您好,我是新来c的,如果有什么问题,请原谅。因此,我可以尝试在调用stat函数之前创建一个char缓冲区[250]和strcat来连接路径?是的,不过使用snprintf(使用%s/%s
作为格式)可能会更容易。OK稍后会尝试。谢谢,你能帮我看看我的编辑吗?我试着按照你说的做,但我还是得到了同样的结果。有什么问题吗?关了。但是(至少)您需要将dir和filename组合起来:snprintf(路径,250,“%s/%s”,“/Library/Logs”,entry->d_name)
如果您检查了stat()
是否成功,您会发现它每次都失败,除非您在/Library/Logs
作为工作目录。而且,您无法明智地分析失败的系统调用返回的数据;关于buf
中的信息,除了它看起来像一个目录条目(但这纯粹是偶然的)之外,您没有什么可说的。有关您还需要做什么的简明摘要,请参见。但是检查每个系统调用的结果是一个很好的开始。嗨,我试过使用你的,我喜欢它是递归的(这就是我计划做的)。但是,当我到达一个以./开头的文件路径时,它将返回一个被拒绝的权限错误,并停止整个程序。struct dirent
的type
成员并非在所有系统上都存在(POSIX并不要求它),并且在存在它的系统上,并非所有文件系统都支持它。因此,建议在每个目录条目上使用stat(2)
,除非您确信您的操作系统和文件系统支持d_类型
成员。@AdamRosenfield嗨,我有点理解您的意思,但是如果我想让这段代码像您提到的那样实现stat(2),该怎么做呢?我不知道如何使用它。对不起,我不太懂这门语言
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
/* "readdir" etc. are defined here. */
#include <dirent.h>
/* limits.h defines "PATH_MAX". */
#include <limits.h>
/* List the files in "dir_name". */
static void
list_dir (const char * dir_name)
{
DIR * d;
/* Open the directory specified by "dir_name". */
d = opendir (dir_name);
/* Check it was opened. */
if (! d) {
fprintf (stderr, "Cannot open directory '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
while (1) {
struct dirent * entry;
const char * d_name;
/* "Readdir" gets subsequent entries from "d". */
entry = readdir (d);
if (! entry) {
/* There are no more entries in this directory, so break
out of the while loop. */
break;
}
d_name = entry->d_name;
/* Print the name of the file and directory. */
printf ("%s/%s\n", dir_name, d_name);
/* See if "entry" is a subdirectory of "d". */
if (entry->d_type & DT_DIR) {
/* Check that the directory is not "d" or d's parent. */
if (strcmp (d_name, "..") != 0 &&
strcmp (d_name, ".") != 0) {
int path_length;
char path[PATH_MAX];
path_length = snprintf (path, PATH_MAX,
"%s/%s", dir_name, d_name);
printf ("%s\n", path);
if (path_length >= PATH_MAX) {
fprintf (stderr, "Path length has got too long.\n");
exit (EXIT_FAILURE);
}
/* Recursively call "list_dir" with the new path. */
list_dir (path);
}
}
}
/* After going through all the entries, close the directory. */
if (closedir (d)) {
fprintf (stderr, "Could not close '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
}
int main ()
{
list_dir ("/Library/Logs");
return 0;
}
/Library/Logs/.<br />
/Library/Logs/..<br />
/Library/Logs/somedir001<br />
/Library/Logs/somedir002