如何使用c++将二进制数据写入windows store应用程序中的文件

How to write Binary data to a file in a windows store app using c++

本文关键字:store windows 应用程序 文件 c++ 何使用 二进制 数据      更新时间:2023-10-16

嘿,我在文件IO上遇到了麻烦。我正在使用一些标准的文件指针的东西,但我一直得到这个错误:在0x58CBC465 (msvcr120_app.dll)在ChemicalWar.exe未处理的异常:一个无效的参数被传递给一个函数,认为无效的参数致命。

现在从我收集到的,我认为这与没有权限写入默认位置有关,但我不确定如何更改位置。

这是目前为止我写的给我带来麻烦的代码:

FILE* ofile;
NametoBinary(_filename);
fopen_s(&ofile, (char*)folder->ToString(), "wb");
fwrite(&animhead, sizeof(Afhead), 1, ofile);
fwrite(binbuff.data(), sizeof(unsigned char), binbuff.size(), ofile);
fclose(ofile);

在第一次fwrite调用时中断。任何帮助都太好了。

所以我想出了解决方案,所以我将把它贴出来,以防其他人需要知道。

FILE* ofile = nullptr;
NametoBinary(_filename);
auto folder = Windows::Storage::ApplicationData::Current->RoamingFolder;
std::wstring ws(folder->Path->Data());
std::string full(ws.begin(), ws.end());
full += "\";
full += _filename;
fopen_s(&ofile, full.c_str(), "wb");
if (nullptr == ofile) return false;
fwrite(&animhead, sizeof(Afhead), 1, ofile);

所以新的windows应用程序有自己的FileI0系统。此解决方案将文件保存在appdata文件夹中的一个文件夹中。