3

I want to validate the existence of a file and after some search I think PathFileExists() may fit the job. However, the following code always show the file doesn't exist. To ensure the file really exist, I choose full path of cmd.exe as the test file path. I'm using windows 7 (x64)

#include "stdafx.h" 
#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#include <WinDef.h>
#pragma comment( lib, "shlwapi.lib")

int _tmain(int argc, _TCHAR* argv[])
{ 

    char path[] = "c:\\Windows\\System32\\cmd.exe";
    LPCTSTR szPath = (LPCTSTR)path;
    if(!PathFileExists(szPath)) 
    { 
        printf("not exist\n");  
    }else{
        printf("exists!\n"); 
    }
    return 0; 
} 

Can you explain about the problem?

UPDATE

Spend almost whole afternoon and figure out the problem. The PathFileExists() function expect second parameter of type LPCTSTR.However, the compiler can not correctly convert char * to LPCTSTR then I include tchar.h and using the TEXTmacro to initialize the pointer. Done. LPCTSTR lpPath = TEXT("c:\Windows\System32\cmd.exe"); The MSDN reference example code for PathFileExists() is kind of outdated. The reference example used char * directly for PathFileExists() and can not pass compile in visual studio 2011 beta. And also , example code missed the using namespace std; Among all, I think @steveha 's answer is closest to the true problem. Thank you all guys.

the final working code look like this:

#include "stdafx.h" 
#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#include <WinDef.h>
#include <tchar.h>
#pragma comment( lib, "shlwapi.lib")

int _tmain(int argc, _TCHAR* argv[])
{ 
    LPCTSTR lpPath = TEXT("c:\\Windows\\System32\\cmd.exe");
        if( PathFileExists(lpPath) == FALSE)  
    { 
        printf("not exist\n");  
    }else{
            printf("exists!\n"); 
    }
    return 0; 
} 

Sorry foradding the solution here, but I really want to post some thought after a whole aternoon work and hope that helps other newbies.

Allan Ruin
  • 5,229
  • 7
  • 37
  • 42
  • 1
    Try calling [GetLastError](http://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx) after the failure, to see what went wrong. – ugoren Jun 11 '12 at 07:58
  • 2
    Please don't apologize for adding the solution here; that is *great*. Now your question will be very helpful for other people who find it here on StackOverflow. My own answer gave you a clue, but isn't a complete answer, and it's great that you posted a complete answer. I already gave you +1 but I would give +1 again if I could. – steveha Jun 11 '12 at 16:43
  • The reference is not outdated; you can specify whether your application uses single byte or unicode strings in project settings (General > Character Set). This affects whether macros such as LPCTSTR expand to `wchar_t const*` or `char const*`, and most API functions can expand to either `PathFileExistsW` or `PathFileExistsA`. – riv Dec 13 '13 at 14:57

1 Answers1

2

I believe the problem is that you need to convert a char string to a TSTR and you aren't doing that.

You are using a type cast to coerce a pointer from type char * to type LPCTSTR but I think that doesn't actually work. As I understand it, a TCHAR is either the same thing as char or else it is a "wide char". I think you must have TCHAR set to wide char or else you wouldn't have a problem.

I found a StackOverflow answer that includes information that may help you:

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms)?

You might try calling MultiByteToWideChar() and see if that fixes the problem.

Community
  • 1
  • 1
steveha
  • 74,789
  • 21
  • 92
  • 117