2

My code is here:

//wstring filename = temp.name + L".txt";
wstring filename = L"hero2.txt";
//wofstream File(filename.c_str());
wofstream outputFile(L"temp.txt", ios::ate | ios::binary);
if (outputFile.fail()) windowmsg(1, LPCWSTR(L"Could not Save game"));
else windowmsg(1, LPCWSTR(L"Game Saved"));

wchar_t BOM = 0xFEFF;//Byte Order Mark character
outputFile << BOM;

outputFile << temp.name << endl;
outputFile << temp.lvl << endl;
outputFile << temp.HP <<endl;
outputFile << temp.minHP <<endl;
outputFile << temp.MP <<endl;
outputFile << temp.minMP <<endl;
outputFile << temp.ATK <<endl;
outputFile << temp.DEF <<endl;
outputFile << temp.mATK <<endl;
outputFile << temp.mDEF <<endl;
outputFile << temp.SPD <<endl;
outputFile << temp.exp <<endl;
outputFile << temp.exptonext <<endl;
outputFile << temp.gearnumber <<endl;
for (int count = 0 ; count < temp.gearnumber ; count++)
{
    outputFile << temp.equip[count].name <<endl;
    outputFile << temp.equip[count].type <<endl;
    outputFile << temp.equip[count].ATK <<endl;
    outputFile << temp.equip[count].DEF <<endl;
    outputFile << temp.equip[count].mATK <<endl;
    outputFile << temp.equip[count].mDEF <<endl;
    outputFile << temp.equip[count].SPD <<endl;
}

I have tried many things, I can't remember them all to state them here. Such as changing the second parameter in fstream to ios::ate | ios::binary and such. I tried converting the empty text file to UTF, with and without BOM, I tried writing the BOM in the C++ code before writing any variables. I always just get a blank file.

Project encoding is Unicode character set. As a note, I really don't care what encoding the file will be, I will only be writing English character text to it. So as long as it writes the file I'm happy.

user1397417
  • 708
  • 4
  • 11
  • 34
  • 1
    The answers to [Wrote to a file using std::wofstream. The file remained empty](http://stackoverflow.com/questions/3950718/wrote-to-a-file-using-stdwofstream-the-file-remained-empty) might help – David L. Nov 23 '12 at 01:51
  • it says i should set std::locale::global(std::locale("Russian_Russia")); but im not using russian characters, its all english. – user1397417 Nov 23 '12 at 01:56
  • Have you done `outputFile .flush()` ? You can also try closing the stream before checking for data in the file... – Michael Shmalko Nov 23 '12 at 02:23
  • just tried adding the outputFile.close(); at the end. i also tried the flush. it doesnt make any difference. – user1397417 Nov 23 '12 at 02:51
  • It depends on your OS and compiler. You may have to use `std::setlocale(LC_ALL, "")` as the first thing in your program (NOT `std::locale::global(anything)`). – n. m. could be an AI Aug 25 '13 at 15:37

0 Answers0