首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wofstream仅创建空文件c++

wofstream仅创建空文件c++
EN

Stack Overflow用户
提问于 2016-07-16 06:43:33
回答 1查看 399关注 0票数 1

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

代码语言:javascript
复制
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();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-16 08:12:40

代码语言:javascript
复制
std::wofstream outFile(convertedPlatformPathandFilename);

这将创建一个新文件,并将其打开以进行写入。

代码语言:javascript
复制
outFile.open(convertedPlatformPathandFilename);

这将尝试打开相同的文件流以进行第二次写入。因为文件流已经打开,所以这是一个错误。该错误会将流设置为失败状态,并且所有写入该流的尝试现在都将失败。

这就是如何得到一个空的输出文件。它被创建,并且第二次重复尝试打开相同的文件流对象会使其进入错误状态。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38405969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档