Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ wofstream只创建一个空文件c++;_C++_Visual Studio_Uwp_Ofstream_Wofstream - Fatal编程技术网

C++ wofstream只创建一个空文件c++;

C++ wofstream只创建一个空文件c++;,c++,visual-studio,uwp,ofstream,wofstream,C++,Visual Studio,Uwp,Ofstream,Wofstream,我有一个for循环(如下),它应该使用wofstream将几个字符串输出到几个文件。不幸的是,它创建了文件,但没有将字符串输出到文件中。文件总是空的。我已经仔细研究了很多类似的问题,但运气不好。任何帮助都将不胜感激。我在一台Windows 10计算机上使用Visual Studio 2015编写UWP应用程序 for (size_t k=0;k < vctSchedulesToReturn.size();k++) { auto platformPath = Windows::Sto

我有一个for循环(如下),它应该使用wofstream将几个字符串输出到几个文件。不幸的是,它创建了文件,但没有将字符串输出到文件中。文件总是空的。我已经仔细研究了很多类似的问题,但运气不好。任何帮助都将不胜感激。我在一台Windows 10计算机上使用Visual Studio 2015编写UWP应用程序

for (size_t k=0;k < vctSchedulesToReturn.size();k++)
{
    auto platformPath = Windows::Storage::ApplicationData::Current->RoamingFolder->Path;
    std::wstring wstrplatformPath = platformPath->Data();
    std::wstring wstrPlatformPathAndFilename = wstrplatformPath + L"\\" + availabilityData.month + L"_" + std::to_wstring(availabilityData.year) + L"_" + std::to_wstring(k) + L"_" + L"AlertScheduleOut.csv";
    std::string convertedPlatformPathandFilename(wstrPlatformPathAndFilename.begin(), wstrPlatformPathAndFilename.end());

    std::wofstream outFile(convertedPlatformPathandFilename);
    outFile.open(convertedPlatformPathandFilename);
    std::vector<std::pair<wstring, wstring>>::iterator pairItr;
    std::wstring strScheduleOutputString = L"";
    for (pairItr = vctSchedulesToReturn[k].second.begin(); pairItr!=vctSchedulesToReturn[k].second.end(); pairItr++)
    {
        strScheduleOutputString += pairItr->first + L",";
    }
    strScheduleOutputString += L"\r\n";
    for (pairItr = vctSchedulesToReturn[k].second.begin(); pairItr != vctSchedulesToReturn[k].second.end(); pairItr++)
    {
        strScheduleOutputString += pairItr->second + L",";
    }
    outFile << strScheduleOutputString;
    outFile.flush();
    outFile.close();
}
for(size_t k=0;kRoamingFolder->Path;
std::wstring wstrplatformPath=platformPath->Data();
std::wstring wstrPlatformPathAndFilename=wstrplatformPath+L“\\”+可用性数据.month+L“\”+std::to_wstring(可用性数据.year)+L“\”+std::to_wstring(k)+L“\+L”AlertScheduleOut.csv”;
std::string convertedPlatformPathandFilename(wstrPlatformPathAndFilename.begin(),wstrPlatformPathAndFilename.end());
std::wofstream输出文件(转换平台路径和文件名);
打开(转换平台路径和文件名);
std::vector::iterator pairItr;
std::wstring strScheduleOutputString=L“”;
对于(pairItr=vctschedulestorurn[k]。second.begin();pairItr!=vctschedulestorurn[k]。second.end();pairItr++)
{
strScheduleOutputString+=pairItr->first+L“,”;
}
strScheduleOutputString+=L“\r\n”;
对于(pairItr=vctschedulestorurn[k]。second.begin();pairItr!=vctschedulestorurn[k]。second.end();pairItr++)
{
strScheduleOutputString+=pairItr->second+L“,”;
}
外锉
这将创建一个新文件,并打开它进行写入

outFile.open(convertedPlatformPathandFilename);
这将尝试打开同一文件流以进行第二次写入。由于文件流已打开,因此这是一个错误。该错误将流设置为失败状态,所有写入流的尝试现在都将失败


这就是你最终得到一个空输出文件的方式。它被创建,重复的第二次尝试打开同一个文件流对象会使它进入错误状态。

你是否尝试过将
输出文件放在一边:你为什么要构造一个字符串并将字符串写入
输出文件
,而不仅仅是写入
输出文件
ans你确定文件是空的吗?我已经看到过几次错误的操作。请发布一个帖子,这样人们就不必猜测
vctScheduleStoreReturn
。Hurkyl谢谢你的帮助。字符串构造只是我故障排除过程中的一个产物,但我不认为它会损害任何东西,所以我就这样保留了它。就是这样。你帮了我几个小时的挫败感。非常感谢!@user2757835:这就是你在做I/O时应该添加错误检查代码的一个很好的原因!Hurkyl,谢谢你的建议。正如你可能知道的,我对任何实质性的软件开发都很陌生。我感谢你的指导。我将添加一些错误检查。
outFile.open(convertedPlatformPathandFilename);