[UWP] [C ++]使用std :: ifstream读取数据 [英] [UWP][C++] read data using std::ifstream

查看:99
本文介绍了[UWP] [C ++]使用std :: ifstream读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道UWP应用程序是沙盒的,这就是为什么你无法访问预设位置以外的文件。


然而,在我的应用程序中,我向用户询问FilePicker他们想要访问的文件夹。通常它会像"C:\ DataImages \"一样。哪个工作正常。我将目录添加到我未来的访问列表中,因此当他们再次启动应用程序时,在稍后的
日期,它仍然可以访问以前选择的文件夹。这很好。


用户对图像执行各种操作后,我想将自定义数据文件与图像文件一起保存。我已经有一个C ++类来处理所有这些,我会缩进重用但它不起作用。


它使用以下内容来读取数据......

 

std :: ifstream readFile(Filename,std :: ios :: in);

readFile.is_open(); //总是失败

我可以使用...枚举所有文件。

 

CreateFileQuery();

GetFilesAsync();

从而确认应用程序可以访问我需要的所有文件。我只是无法使用std :: ifstream读取它们。


解决方案

这是预期的行为(目前)。您的应用程序无法直接访问这些文件,只能通过StorageItem API通过文件代理访问它们。请参阅我的博客条目  https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/ 


在当前版本中,您可以通过 $ b $从StorageFile获取一个HANDLE。 b IStorageHandleAccess (错误地标记为仅限桌面,但可从UWP调用)并将其与Win32文件API一起使用(请参阅http://stackoverflow.com/questions/42799235/how-can-i-get-a- win32-handle-for-a-storagefile-or-storagefolder-in-uwp)


正如昨天宣布的那样,在下一个版本的Windows中,你将能够直接使用Win32文件API :CreateFile2将打开应用程序直接访问的路径。


通过https://查看Build 2017会话  UWP Apps文件访问权限 channel9.msdn.com/events/Build/2017/P4102


I understand that UWP apps are sandboxed that's why you cannot access files outside of pre-set locations.

However, in my app I ask the user with a FilePicker which folder they want to access. Typically it will be something like "C:\DataImages\" which works fine. I add the directory to my future access list so when they launch the app again, at a later date, it can still access the previously selected folder. This works fine.

After the user performs various actions on the images I want to save a custom data file along with the image file. I already have a C++ class that handles all this that I indent to reuse but it won't work.

It uses the following to read data...

std::ifstream readFile(Filename, std::ios::in);

readFile.is_open(); //always fails

I can enumerate all the files using...

CreateFileQuery();

GetFilesAsync();

thus confirming the app has access to all the files I need. I just cannot read them using std::ifstream.

解决方案

That's expected behavior (for now). Your app doesn't have direct access to these files and can access them only through the file broker via the StorageItem API. See my blog entry https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/ 

In current versions you can get a HANDLE from a StorageFile via IStorageHandleAccess (incorrectly flagged as desktop only, but callable from UWP) and use that with Win32 file API (see http://stackoverflow.com/questions/42799235/how-can-i-get-a-win32-handle-for-a-storagefile-or-storagefolder-in-uwp )

As announced yesterday, in the next version of Windows you'll be able to use Win32 file API directly: CreateFile2 will open paths that the app has brokered access to directly.

Check out the Build 2017 session UWP Apps file access improvements at https://channel9.msdn.com/events/Build/2017/P4102


这篇关于[UWP] [C ++]使用std :: ifstream读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆