如何检查文件是否在给定目录中

时间:2019-04-06 21:32:06

标签: c file directory cygwin subdirectory

我试图用很多单词进行搜索,但是由于某种原因,我只得到了诸如“如何检查文件或目录是否存在”之类的问题的参考。 相反,我想检查给定文件是否位于给定目录中。

问题在于文件,目录或两者都可能有时甚至必须作为相对路径而不是绝对路径传递。

是否有任何Windows / unix函数对此进行检查?

2 个答案:

答案 0 :(得分:1)

下面是使用stat()函数的示例,无论'filename'是完整路径还是仅仅是文件名

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

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



#if 0
           struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               nlink_t   st_nlink;       /* Number of hard links */
               uid_t     st_uid;         /* User ID of owner */
               gid_t     st_gid;         /* Group ID of owner */
               dev_t     st_rdev;        /* Device ID (if special file) */
               off_t     st_size;        /* Total size, in bytes */
               blksize_t st_blksize;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */

               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */

           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };
#endif

bool doesFileExistInDir( char *path, char *filename )
{
    struct stat myStat;
    int statStatus;

    char pathname[ strlen(path) + strlen( filename ) + 1];

    if( !strchr( filename, '/' ) )
    { 
        pathname[0] = '\0';
        strcat( pathname, path );
        // strcat( pathname, "/" );
        strcat( pathname, filename );
    }
    else
    {
        strcpy( pathname, filename );
    }

    if( (statStatus = stat( pathname, &myStat )) != 0 )
    {
        // then file not accessible -or- directory not readable -or- file does not exist
        perror( "stat failed" );
        return false;
    }

    return true;
}

答案 1 :(得分:0)

此功能可用于确定dirname是否是filename的目录。

int file_is_in_directory (char *filename, char *dirname) /* [!] Windows-specific */
{
    char   filename_full[MAX_PATH] = {'\0'};
    char   dirname_full[MAX_PATH]  = {'\0'};
    char*  file                    = NULL;

    GetFullPathNameA(filename,  MAX_PATH,   filename_full,  NULL); /* [!] Check rval */
    GetFullPathNameA(dirname,   MAX_PATH,   dirname_full,   NULL); /* [!] Check rval */

    if(!strncmp(dirname_full, filename_full, strlen(dirname_full)))
    {
        return 1; /* File is located in directory */
    }

    return 0; /* File not contained */
}