简体   繁体   English

无法在Visual Studio窗口中打开文件

[英]not opening file in visual studio windows

I am running Windows 10 and using VS2017. 我正在运行Windows 10并使用VS2017。 The code below is not opening a file... 以下代码无法打开文件...

    std::ofstream output_file;
    output_file.open("datafile.txt", std::ios::out);
    if (!output_file.is_open())
    {
        std::cout << "This is the whole problem";
    }

Could this be related to permission to writing/reading file and how can I give it the permission. 这可能与写入/读取文件的权限有关,如何授予该权限。

In UWP app, it can access certain file system locations by default. 在UWP应用中,默认情况下它可以访问某些文件系统位置。 It can also access additional locations through the file picker , or by declaring capabilities . 它还可以通过文件选择器或声明功能来访问其他位置。 You can see the File access permissions topic for details. 您可以查看“ 文件访问权限”主题以了解详细信息。

For your issue, it seems you want to access the file in your app InstalledLocation, as the Application install directory introduction, you can access the file using the following code in C++/WinRT: 对于您的问题,似乎您想访问应用程序InstalledLocation中的文件,作为应用程序安装目录的介绍,您可以使用C ++ / WinRT中的以下代码来访问文件:

Windows::Foundation::IAsyncAction ExampleCoroutineAsync()
{
    Windows::Storage::StorageFile file{
        co_await Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(Windows::Foundation::Uri{L"ms-appx:///datafile.txt"})
    };
    // Process file
}

Or C++: 或C ++:

auto getFileTask = create_task(StorageFile::GetFileFromApplicationUriAsync(ref new Uri("ms-appx:///datafile.txt")));
getFileTask.then([](StorageFile^ file) 
{
    // Process file
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM