[英]Checking whether path is a file or directory
I just started learning C and am trying to do path listing. 我刚刚开始学习C并且正在尝试进行路径列表。 I have tried to list the path in the directory using
dirent
and tried to check if the result is a file or directory, using stat
. 我试图使用
dirent
列出目录中的路径,并尝试使用stat
检查结果是文件还是目录。 However, even if the path is a file, it will return every path as a directory. 但是,即使路径是文件,它也会将每个路径作为目录返回。
This is my code: 这是我的代码:
[Edit] [编辑]
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
int main(void)
{
DIR *mydir = opendir("/Library/Logs");
char path[250];
struct dirent *entry = NULL;
struct stat buf;
while((entry = readdir(mydir))) /* If we get EOF, the expression is 0 and
* the loop stops. */
{
snprintf(path, 250, "%s",entry->d_name);
stat(path,&buf);
if(S_ISDIR(buf.st_mode))
printf("D: %s\n", path);
else if (S_ISREG(buf.st_mode))
printf("F: %s\n", path);
else
printf("O: %s\n", path);
}
}
stat(entry->d_name,&buf);
At this point, you don't have any context about the directory you're looking in. 此时,您没有关于您正在查找的目录的任何上下文。
You will need to create a buffer, and concatenate your directory
/ filename
before calling stat
(using strcat
or snprintf
) 您需要创建一个缓冲区,并在调用
stat
之前连接directory
/ filename
(使用strcat
或snprintf
)
Checking the call to stat
's return value -- if non-zero, look at errno
to see what went wrong. 检查对
stat
的返回值的调用 - 如果非零,请查看errno
以查看出错的地方。 I'm guessing it's currently reporting an ENOENT. 我猜它现在正在报告ENOENT。
Try this, using readdir and dirent 尝试使用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;
}
The output will be 输出将是
/Library/Logs/.<br />
/Library/Logs/..<br />
/Library/Logs/somedir001<br />
/Library/Logs/somedir002
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.