std::ifstream::open() 在 Windows 10 个通用应用程序中失败

std::ifstream::open() fails in Windows 10 Universal apps

我正在开发一个 Windows 10 通用 C++ 项目,我正在尝试使用 std::ifstream 在读取模式下打开一个二进制文件.

这是我的代码:

std::ifstream imgFile("C:\Users\GuiTeK\Desktop\picture.bmp", std::ios::binary);

if (imgFile.is_open())
{
    std::cout << "OK" << std::endl;
}

else
{
    int error = errno;
    std::cerr << "KO: " << error << std::endl;
}

问题是它一直失败并显示 错误 13,这意味着“数据无效”(C.F。 System Error Codes).

但是,完全相同的代码 在 Win32 控制台应用程序 C++ 项目中工作正常

怎么了?

UWP 应用无权访问设备上的所有文件。默认情况下,应用程序可以访问某些文件系统位置,例如应用程序安装目录或应用程序数据位置。更多信息,请参阅File access permissions

"C:\Users\GuiTeK\Desktop\picture.bmp" 是您的应用无法直接访问的位置。在 UWP 中,我们需要一个 File​Open​Picker to access such a file. One important rule here is that Skip the path: stick to the StorageFile.

有关如何在 UWP 中处理文件的更多信息,请参阅 GitHub 上的 Files, folders, and libraries and also File access sample, File picker sample