检查文件名是否是C中的目录 [英] Check to see if a filename is a directory in C

查看:77
本文介绍了检查文件名是否是C中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是列出目录中的所有文件.
如果显示了目录,则它还应该打印出该目录中的文件.

目前,我有:

What I have to do is list all the files within a directory.
If a directory is shown then it also should print out the files within that directory.

At the moment I have:

DIR *directory;
struct dirent *files;
char folderPath[] = "//home//unix//20//";


 // Make sure I can open the folder "test" otherwise throw an error
     if ((directory = opendir(folderPath)) == NULL)
     {
        printf("\nCouldn't Open Folder!\n");
        return 0;
     }
	// Loop around and check if the directory is another directory
	int i = 0;
		while ((files = readdir(directory)) != NULL)
		{
		  //printf("%s\n", files->d_name);
		  if (files->d_name 
		  i++;
		}
	printf("\n");



此代码将打印出该目录中的所有文件和文件夹.

我现在需要做的就是找出d_name是否为目录,如果是,则打印出该目录中的内容.

有人可以帮助我如何做到这一点吗?
Thank-You



This code prints out all the files and folders within that directory.

What I need to do now, is find out if d_name is a directory and if it is, print out the contents within that directory.

Can some-one help me on how I can do this?
Thank-You

推荐答案

您必须查看文件属性...第一个字段中的"d"表示目录:
http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilesp.html [ ^ ]

我相信stat()将为您提供linux中的属性:
http://pubs.opengroup.org/onlinepubs/000095399/functions/stat.html [ ^ ]
You have to look at the file attributes... A ''d'' in the first field denotes a directory:
http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilesp.html[^]

I believe stat() will give you the attributes in linux:
http://pubs.opengroup.org/onlinepubs/000095399/functions/stat.html[^]


检查是否d_type & DT_DIR,如果是,则为目录.
Check if d_type & DT_DIR and if so it is a directory.


这篇关于检查文件名是否是C中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆