Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a for loop (below) that should output several strings to several files using wofstream. Unfortunately, it creates the file but does not output the string to the file. The files are always empty. I've looked through a lot of similar questions with no luck. Any help would be greatly appreciated. I'm on a Windows 10 machine using Visual Studio 2015 to write a UWP app.

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();
                Aside: why are you constructing a string and writing the string to outFile rather than just writing to outFile?
– user1084944
                Jul 15, 2016 at 22:54
                Also, by what means are you determining the file is empty? I have seen that done wrongly a few times.
– user1084944
                Jul 15, 2016 at 22:56
                Please post an MCVE so people don't have to guess what is actually inside vctSchedulesToReturn.
– Michael Burr
                Jul 15, 2016 at 22:58
                Hurkyl Thanks for the help. the string construction was just an artifact of my troubleshooting but I didn't think it was hurting anything so I left it that way.
– ViperJuice
                Jul 18, 2016 at 7:59

This creates a new file, and opens it for writing.

outFile.open(convertedPlatformPathandFilename);

This attempts to open the same file stream for writing a second time. Because the file stream is already open, this is an error. The error sets the stream into a failed state, and all attempts to write to the stream will now fail.

This is how you end up with an empty output file. It gets created, and a duplicate, second attempt to open the same file stream object puts it into error state.

@user2757835: That's one good reason why you should add error checking code when doing I/O! – user1084944 Jul 17, 2016 at 4:48 Hurkyl, Thanks for the advice. As you can probably tell, I'm pretty new to any kind of substantial software development. I appreciate the guidance. I'll add some error checking. – ViperJuice Jul 18, 2016 at 8:12

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.