2017-06-23 103 views
-1

我正在研究Windows 10通用C++項目,我試圖用std::ifstream以讀取模式打開二進制文件。std :: ifstream :: open()在Windows 10通用應用程序中失敗

這是我的代碼:

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++項目中正常工作

怎麼了?

+1

你的應用程序的清單請求有什麼權限? –

回答

2

UWP應用程序沒有權限訪問設備上的所有文件。默認情況下,應用可以訪問某些文件系統位置,如應用程序安裝目錄或應用程序數據位置。欲瞭解更多信息,請參閱。

"C:\Users\GuiTeK\Desktop\picture.bmp"是您的應用無法直接訪問的位置。在UWP中,我們需要一個File​Open​Picker來訪問這樣的文件。這裏的一個重要規則是Skip the path: stick to the StorageFile

有關如何處理UWP文件的詳細信息,請參閱GitHub上的Files, folders, and libraries以及File access sampleFile picker sample

相關問題