UWP - 使用 C 代码编写文件

问题描述

我有一个 C++ UWP 应用,它调用一个 C 函数来写入一个文件,例如

FILE * fp = fopen ("file.txt","w+");
fprintf(fp,"Hello");
fclose(fp);

fopen() 失败并显示错误 13,这意味着对该目录的权限不足。如何确保 C 函数可以写入文件

解决方法

由于 UWP 在沙箱中运行,我通过在 C++ 中获取可写文件路径来解决它:

StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
String^ path = localFolder->Path;

然后我将路径传递给 C 程序。