开发者

program to validate directory name

I just want to write a program which takes a directory name as argument

开发者_高级运维
  • Validate that it is in fact a directory
  • Get a listing of all files in the directory and print it


The word directory doesn't even appear in the C standard. This is an OS concept.


Look at stat. It will provide you with the information you want; all you have to do is interpret it.

Edit: A brief example.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

#define TRUE 1
#define FALSE 0

int is_dir(char *path)
{
    struct stat dir_stats;

    stat(path, &dir_stats);
    if (S_ISDIR(dir_stats.st_mode))
        return TRUE;
    return FALSE;
}

For the list of files in the directory, use readdir.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜