1
  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?

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

I want to check if directory path is existed or not. Please provide one sample code. Thank you.

SHRI
  • 2,406
  • 6
  • 32
  • 48
  • 1
    Try calling the narrow version of FindFirstFile, `FindFirstFileA`, and use the narrow version of the structure, `WIN32_FIND_DATAA` – WhozCraig Sep 15 '13 at 06:39
  • @WhozCraig Thanks allot. I could pass char pointer directly now. But problem is, I passed "C:\\" and '&FindFileData' to this API and it gave return code 0xFFFFFFFF, that means not found right? The path C:\ existed in my local drive. – SHRI Sep 15 '13 at 06:47
  • 1
    It means not found because that isn't the *directory*, its just a path to one. You're not finished yet. Try `"C:\\."` – WhozCraig Sep 15 '13 at 06:49
  • @WhozCraig You are right, I created a directory called 'xx' and gave path as `"C:\\xx"` and it worked. But I also tried giving `"C:\\."`, but it did not work. – SHRI Sep 15 '13 at 07:12
  • If you want to check if a drive letter exists you can use `GetLogicalDrives`. – Jonathan Potter Sep 15 '13 at 11:48
  • The answer is in a different question: [C++ `PathFileExists` limited to 260 chars](http://stackoverflow.com/questions/13877994/c-pathfileexists-limited-to-260-chars). – IInspectable Sep 15 '13 at 11:59

4 Answers4

2

You can simply use access, which is widely supported on Windows, Linux, Mac, etc: access(filepath, 0) returns 0 if file exists, and error code otherwise. On Windows, you will need to #include <io.h>.

mvp
  • 111,019
  • 13
  • 122
  • 148
0

How about doing it like this using getAttributes function:-

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

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

or you can try this too:-

#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;
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You can check whether the path is correct or not without using any Windows API as shown below

    /*
     * 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;
    }
Vivek S
  • 1,251
  • 10
  • 12
  • And if the directory path is valid but the file `"test.txt"` isn't there? – WhozCraig Sep 15 '13 at 06:59
  • @WhozCraig - You are right, the second argument should be "w" instead of "r". Thanks for pointing it out. – Vivek S Sep 15 '13 at 07:07
  • Even changing it to `"w"` isn't going to work if you don't have write-permission on the folder. You need a non-intrusive method. – WhozCraig Sep 15 '13 at 08:01
0
#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;
}
Megharaj
  • 1,589
  • 2
  • 20
  • 32