为什么 PathFileExists() 不起作用? [英] Why PathFileExists() not working?
问题描述
我想验证文件是否存在,经过一些搜索,我认为 PathFileExists() 可能适合该工作.但是,以下代码始终显示该文件不存在.为了确保文件确实存在,我选择cmd.exe的完整路径作为测试文件路径.我使用的是 Windows 7 (x64)
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?
花了几乎整个下午的时间来找出问题所在.PathFileExists() 函数需要 LPCTSTR
类型的第二个参数.但是,编译器无法将 char *
正确转换为 LPCTSTR
然后我包含 tchar.h
并使用 TEXT
宏来初始化指针.完毕.LPCTSTR lpPath = TEXT("c:\Windows\System32\cmd.exe");PathFileExists 的 MSDN 参考示例代码() 有点过时了.参考示例直接将 char *
用于 PathFileExists() 并且无法在 Visual Studio 2011 beta 中通过编译.而且,示例代码错过了 using namespace std;
其中,我认为@steveha 的答案最接近真正的问题.谢谢大家.
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 TEXT
macro 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.
推荐答案
我认为问题在于您需要将 char
字符串转换为 TSTR
而您不要那样做.
I believe the problem is that you need to convert a char
string to a TSTR
and you aren't doing that.
您正在使用类型转换将指针从 char *
类型强制转换为 LPCTSTR
类型,但我认为这实际上不起作用.据我了解, TCHAR
要么与 char
相同,要么就是宽字符".我认为您必须将 TCHAR
设置为宽字符,否则您不会有问题.
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.
我找到了一个 StackOverflow 答案,其中包含可能对您有帮助的信息:
I found a StackOverflow answer that includes information that may help you:
什么在 C/C++(ms) 中将 char[] 转换为/从 tchar[] 的最简单方法是什么?
您可以尝试调用 MultiByteToWideChar()
并查看是否可以解决问题.
You might try calling MultiByteToWideChar()
and see if that fixes the problem.
这篇关于为什么 PathFileExists() 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!