I want to write data stored in a vector into a file. Therefore I use the following routine:
bool Grid::saveToFile() {
stringstream sstream;
for (size_t i = 0; i < taglist.size(); ++i)
{
if (i != 0)
sstream << ",";
sstream << taglist[i];
}
string s = sstream.str();
CFileDialog FileDlg(FALSE);
if (FileDlg.DoModal() == IDOK) {
CString pathName = FileDlg.GetPathName();
CStdioFile outputFile(pathName, CFile::modeWrite | CFile::modeCreate);
outputFile.WriteString((LPCTSTR)s.c_str());
outputFile.Close();
return TRUE;
}
return FALSE;
}
The problem is: Although there's data in s, the output file is always NULL. Can anybody solve that mystery?
New MFC projects are created as Unicode, so I assume this is Unicode.
Also your use of
(LPCTSTR)suggests you are getting an error and you try to fix by casting (it doesn't work)You should create the file as Unicode, and use wide string
std::functions such asstd::wstringorstd::wstringstreamExample:
Edit
By the way, you can also use
std::wofstreamwithpubsetbufto write directly to stream in UnicodeAnd similarly use
std::wifstreamto open the stream