Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果存在无法在C+中工作的文件,请检查绝对文件路径+;_C++_Windows_Ifstream - Fatal编程技术网

C++ 如果存在无法在C+中工作的文件,请检查绝对文件路径+;

C++ 如果存在无法在C+中工作的文件,请检查绝对文件路径+;,c++,windows,ifstream,C++,Windows,Ifstream,我知道有类似的问题,但这并没有解决我的问题 我使用了std::ifstream和PathFileExist,但它总是返回false,并且我的错误处理程序GetLastError()总是给出消息 "The system cannot find the file specified" 我正在使用Visual Studio 2012,我已经使用绝对路径测试了不同目录中的文件,以消除“权限”和“错误路径”问题的可能性 这是我的代码片段 注意:ErrorExit只是一个使用GetLastError()显

我知道有类似的问题,但这并没有解决我的问题

我使用了std::ifstream和PathFileExist,但它总是返回false,并且我的错误处理程序GetLastError()总是给出消息

"The system cannot find the file specified"
我正在使用Visual Studio 2012,我已经使用绝对路径测试了不同目录中的文件,以消除“权限”和“错误路径”问题的可能性

这是我的代码片段

注意:ErrorExit只是一个使用GetLastError()显示错误消息的包装器

尝试

if (std::ifstream((LPCWSTR)"C:\\Windows\\write.exe"))
{
    // do some thing if file is found
}   
else
    ErrorExit(TEXT("ifstream"));
或者尝试B

std::ifstream f;
f.open(L"C:\\Windows\\write.exe", ios::in);

if (f.is_open())
{
    // do something if file is open
}
或者在第一次尝试C时检查权限

if (_waccess(L"C:\\Windows\\write.exe", 04) == 0)

    if (std::ifstream((LPCWSTR)"C:\\Windows\\write.exe"))
    {
         // do some thing if file is found
    }   
    else
        ErrorExit(TEXT("ifstream"));
}
else
    ErrorExit("_waccess");
我已经尝试过不同的字符串约定,如:

L"C:\\Windows\\write.exe"
(LPCWSTR)"C:\\Windows\\write.exe"
_T("C:\\Windows\\write.exe")
TEXT("C:\\Windows\\write.exe")
还是不走运


提前感谢。

我知道您有windows特定的代码(带有LPCWSTR等)

所以请使用WinAPI

#include <windows.h>
...
if (GetFileAttributesW(L"C:\\windows\\write.exe") == -1)
  file_not_found(); // file does not exist
else
  file_exists(); // do some thing if file is found
#包括
...
如果(GetFileAttributesW(L“C:\\windows\\write.exe”)==-1)
未找到文件();//文件不存在
其他的
文件_exists();//如果找到文件,请执行某些操作

我知道您有windows特定的代码(带有LPCWSTR等)

所以请使用WinAPI

#include <windows.h>
...
if (GetFileAttributesW(L"C:\\windows\\write.exe") == -1)
  file_not_found(); // file does not exist
else
  file_exists(); // do some thing if file is found
#包括
...
如果(GetFileAttributesW(L“C:\\windows\\write.exe”)==-1)
未找到文件();//文件不存在
其他的
文件_exists();//如果找到文件,请执行某些操作

您的项目是UNICODE吗?@sameerkn是的。停止播放。摆脱
(LPCWSTR)
强制转换。您并不是通过施放将非宽弦神奇地变成宽弦。使用正确的字符串类型——当您强制转换时,您所做的一切就是关闭编译器,以防出现错误。@PaulMcKenzie,正如您在我尝试的语句中所看到的,即使没有强制转换也不起作用。使用
wifstream
而不是
ifstream
。您的项目是UNICODE吗?@sameerkn是的。停止强制转换。摆脱
(LPCWSTR)
强制转换。您并不是通过施放将非宽弦神奇地变成宽弦。使用正确的字符串类型——强制转换时所做的一切就是关闭编译器,使其停止处理错误。@PaulMcKenzie,正如我尝试过的语句中所示,即使没有强制转换也不起作用。使用
wifstream
而不是
ifstream