【问题标题】:How to check if a file is in given directory如何检查文件是否在给定目录中
【发布时间】:2019-08-28 10:27:33
【问题描述】:

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

问题是文件、目录或两者都可以而且有时必须作为相对路径而不是绝对路径传递。

是否有任何 windows/unix 函数可以检查它?

【问题讨论】:

  • 我无法理解其中的区别。使用文件路径检查文件是否存在是您确定文件是否位于该目录中的方法。如果它不“存在”,那么它就不在那里。你问的是不同的东西吗?
  • @MarkBenningfield 如果文件显示为绝对路径怎么办?它需要检测到并将其标记为路径和文件,或者将其与最后一个 `\` 与提供的路径进行比较(如果它是绝对路径)。
  • 如果您使用某些函数将相对路径解析为绝对路径,请确保两者都已解析然后使用strncpy,但我想知道是否有标准函数这样做。
  • 绝对路径文件路径。我所知道的几乎所有决定文件是否存在的 API 都使用绝对路径,因为这是文件的实际名称。为什么(以及如何)只使用文件名?
  • 不需要标记它。所有检查函数都需要一个路径,该路径是绝对的或相对的,如果是相对的,则可能不包含任何斜杠(或反斜杠)。您是否需要区分常规文件和目录、符号链接、套接字、FIFO、字符和块特殊设备?还是名称只需要存在?能不能打开文件有关系吗?

标签: c file directory cygwin subdirectory


【解决方案1】:

这里是使用stat() 函数的示例,无论“文件名”是完整路径还是只是文件名

#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;
}

【讨论】:

  • 如果@filename 是绝对路径,则不起作用:) 但是+1 是使用stat 的示例。我经常使用stat 来获取文件的完整性。
  • 这是核心问题。检查目录中是否存在文件很容易,但如果文件显示为绝对路径,则变得更加棘手。如果您阅读问题正文而不仅仅是问题标题,您会发现它被精确地询问,并倾向于这样的场景。
【解决方案2】:

这个函数可以用来判断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 */
}

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 2016-02-01
    • 2010-11-04
    • 2012-03-19
    • 2014-02-18
    • 2014-03-16
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    相关资源
    最近更新 更多