【问题标题】:Check if file existing or not in c检查文件是否存在于c中
【发布时间】:2013-09-19 12:22:58
【问题描述】:
  1. prgchDirPath 是字符指针。但预期的是 LPCWSTR。 如何更改?
  2. prgchDirPath 是目录路径。 文件不存在。 但我想确定目录/路径是否存在。 下面的 API 可以帮助我吗?如果是,怎么做?

    unsigned char IsFilePathCorrect(char* prgchDirPath)
    {
            WIN32_FIND_DATA FindFileData;
            HANDLE handle;
            int found=0;
    
            //1. prgchDirPath is char pointer. But expected is LPCWSTR.
            //How to change this?
            //2. prgchDirPath is a directory path. File is not existed.
            //But I want to make sure if directory/path exists or not.
            //Can the API below helps me? if yes, how?
            handle = FindFirstFile(prgchDirPath, &FindFileData);
            if(handle != INVALID_HANDLE_VALUE)
            found = 1;
            if(found) 
            {
                FindClose(handle);
            }
            return found;
    }
    

我想检查目录路径是否存在。请提供一个示例代码。谢谢。

【问题讨论】:

  • 尝试调用 FindFirstFile 的窄版 FindFirstFileA,并使用结构的窄版 WIN32_FIND_DATAA
  • @WhozCraig 感谢分配。我现在可以直接传递 char 指针。但问题是,我将 "C:\\" 和 '&FindFileData' 传递给了这个 API,它给出了返回码 0xFFFFFFFF,这意味着没有找到,对吧?路径 C:\ 存在于我的本地驱动器中。
  • 这意味着没有找到,因为那不是目录,它只是一个路径。你还没有完成。试试"C:\\."
  • @WhozCraig 你说得对,我创建了一个名为 'xx' 的目录,并将路径指定为 "C:\\xx" 并且它有效。但是我也试过给"C:\\.",但是没用。
  • 如果要检查驱动器号是否存在,可以使用GetLogicalDrives

标签: c file winapi widechar lpcwstr


【解决方案1】:

使用getAttributes函数来做这样的事情怎么样:-

BOOL DirectoryExists(LPCTSTR szPath)
{
  DWORD dwAttrib = GetFileAttributes(szPath);

  return (dwAttrib != INVALID_FILE_ATTRIBUTES && 
         (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

或者你也可以试试这个:-

#include <io.h>     // For access().
#include <sys/types.h>  // For stat().
#include <sys/stat.h>   // For stat().

bool DirectoryExists( const char* path){

    if( _access( path, 0 ) == 0 ){

        struct stat s;
        stat( path, &s);

        return (s.st_mode & S_IFDIR) != 0;
    }
    return false;
}

【讨论】:

    【解决方案2】:

    您可以在不使用任何 Windows API 的情况下检查路径是否正确,如下所示

        /*
         * Assuming that prgchDirPath is path to a directory
         * and not to any file.
         */
        unsigned char IsFilePathCorrect(char* prgchDirPath)
        {
            FILE *fp;
            char path[MAX_PATH] = {0};
    
            strcpy(path, prgchDirPath);
            strcat(path, "/test.txt");
    
            fp = fopen(path, "w");
            if (fp == NULL)
                return 0;
    
            fclose(fp);
            return 1;
        }
    

    【讨论】:

    • 如果目录路径有效但文件"test.txt"不存在?
    • @WhozCraig - 你是对的,第二个参数应该是“w”而不是“r”。感谢您指出。
    • 如果您没有对该文件夹的写权限,即使将其更改为 "w" 也不起作用。您需要一种非侵入式方法。
    【解决方案3】:

    您可以简单地使用access,它在 Windows、Linux、Mac 等平台上得到广泛支持:access(filepath, 0) 如果文件存在则返回 0,否则返回错误代码。在 Windows 上,您需要#include &lt;io.h&gt;

    【讨论】:

      【解决方案4】:
      #include<stdio.h>
      #include<fcntl.h>
      
      #define DEVICE "test.txt"
      
      int main()
      {
          int value = 0;
          if(access(DEVICE, F_OK) == -1) {
              printf("file %s not present\n",DEVICE);
              return 0;
          }
          else
              printf("file %s present, will be used\n",DEVICE);
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-04
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        • 2011-05-14
        • 1970-01-01
        • 2012-05-03
        • 2011-07-01
        • 2020-03-20
        相关资源
        最近更新 更多