1

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();
}
ViperJuice
  • 13
  • 5
  • 2
    have you tried `outFile << "Hello World";` just to check what's going on? – tony Jul 15 '16 at 22:46
  • 1
    Aside: why are you constructing a string and writing the string to `outFile` rather than just writing to `outFile`? –  Jul 15 '16 at 22:54
  • Also, by what means are you determining the file is empty? I have seen that done wrongly a few times. –  Jul 15 '16 at 22:56
  • 2
    Please post an [MCVE](http://stackoverflow.com/help/mcve) so people don't have to guess what is actually inside `vctSchedulesToReturn`. – Michael Burr Jul 15 '16 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 '16 at 07:59

1 Answers1

3
std::wofstream outFile(convertedPlatformPathandFilename);

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.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • That was it. You just saved me hours of frustration. Thank you so much! – ViperJuice Jul 16 '16 at 00:28
  • @user2757835: That's one good reason why you should add error checking code when doing I/O! –  Jul 17 '16 at 04: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 '16 at 08:12