2016-07-15 14 views
1

wofstreamを使用していくつかの文字列を複数のファイルに出力する必要があるforループ(下記)があります。残念ながら、ファイルを作成しますが、文字列をファイルに出力しません。ファイルは常に空です。私は運がない、似たような質問をたくさん見てきました。どんな助けでも大歓迎です。私はUWPアプリケーションを書くためにVisual Studio 2015を使っているWindows 10マシン上にいる。wofstreamは空のファイルのみを作成します。

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(); 
} 
+2

あなたは 'outFile <<" Hello World ";試してみましたか? – tony

+1

別名:なぜあなたは文字列を構築し、 'outFile'に書き込むのではなく' outFile'に文字列を書きますか? – Hurkyl

+0

また、どのような手段でファイルが空であると判断していますか?私はそれが間違って何回か行われたのを見ました。 – Hurkyl

答えて

3
std::wofstream outFile(convertedPlatformPathandFilename); 

これは新しいファイルを作成し、書き込みのためにそれを開きます。

outFile.open(convertedPlatformPathandFilename); 

これは、同じファイルストリームをもう一度書き込むために開こうとします。ファイルストリームは既に開いているため、これはエラーです。このエラーは、ストリームを失敗した状態に設定し、ストリームへのすべての書き込みが失敗するようになりました。

この方法で、出力ファイルが空になります。それが作成され、同じファイルストリームオブジェクトを開くための重複した2回目の試行がエラー状態になります。

+0

それでした。あなたは私に数時間の欲求不満を救った。どうもありがとうございます! – ViperJuice

+0

@ user2757835:I/Oを実行するときにエラーチェックコードを追加する理由があります。 – Hurkyl

+0

ハルキル、アドバイスをいただきありがとうございます。ご存じのように、私はあらゆる種類の実質的なソフトウェア開発にはまったく新しいものです。私は指導に感謝します。私はいくつかのエラーチェックを追加します。 – ViperJuice

関連する問題