为要保存的文件添加扩展名
Add an extention to the file you want to save
问题是我保存的时候需要给文件名加上扩展名。 outfile.open(filename + ".txt")
之类的解决方案似乎不适用于我的情况。
有我的代码:
SaveFileDialog* saveFileDialog1 = new SaveFileDialog();
int saveAs(const string& outFileName)
{
string bufFile("C:\Windows\tmp.XXXXXX");
string outFile(saveFileDialog1->NewFileName);
string line;
string val;
ifstream buf_stream;
ofstream out_stream;
buf_stream.open(bufFile.c_str());
out_stream.open(outFile.c_str());
if (buf_stream)
{
while (!buf_stream.eof())
{
getline(buf_stream, line);
buf_stream >> val;
out_stream << val<<endl;
}
}
buf_stream.close();
out_stream.close();
remove("C:\Windows\tmp.XXXXXX");
return 0;
}
然后当我想保存结果时,我尝试使用该构造:
case IDM_FILE_SAVE:
{
saveFileDialog1;
saveFileDialog1->ShowDialog();
saveFileDialog1->FilterIndex1 = 1;
saveFileDialog1->Flags1 |= OFN_SHOWHELP;
saveFileDialog1->InitialDir1 = _T("C:\Windows\");
saveFileDialog1->Title1 = _T("Save File");
int retval = saveAs(saveFileDialog1->NewFileName);
}
这是我解决问题的尝试
SaveFileDialog::SaveFileDialog(void)
{
this->DefaultExtension1 = 0;
this->NewFileName = new TCHAR[MAX_PATH + TCHAR(".txt")];
this->FilterIndex1 = 1;
this->Flags1 = OFN_OVERWRITEPROMPT;
this->InitialDir1 = 0;
this->Owner1 = 0;
this->Title1 = 0;
this->RestoreDirectory = true;
}
bool SaveFileDialog::ShowDialog()
{
OPENFILENAME ofn;
TCHAR szFile[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = this->Owner1;
ofn.lpstrDefExt = this->DefaultExtension1;
ofn.lpstrFile = this->NewFileName;
ofn.lpstrFile[0] = '[=12=]';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = _T("All Files[=12=]*.*[=12=]Text files[=12=]*.txt");
ofn.nFilterIndex = this->FilterIndex1;
ofn.lpstrInitialDir = this->InitialDir1;
ofn.lpstrTitle = this->Title1;
ofn.Flags = this->Flags1;
GetSaveFileName(&ofn);
if (_tcslen(this->NewFileName) == 0) return false;
return true;
}
如有任何建议,我们将不胜感激。
使用 c++11(检查您的编译器标志?)字符串上的 fstream::open()
function works perfectly well with strings. So no need to pass through c_str()
. Then you can use the operator+
如您所愿:
buf_stream.open(bufFile);
out_stream.open(outFile);
...
outfile.open(filename + ".txt");
备注:如果在声明时文件名已知,您可以立即将文件名提供给流的构造函数。因此不需要调用单独的 open()
.
如果不能使用C++11,则使用临时字符串进行连接:
outfile.open (string(filename+".tst").c_str());
问题是我保存的时候需要给文件名加上扩展名。 outfile.open(filename + ".txt")
之类的解决方案似乎不适用于我的情况。
有我的代码:
SaveFileDialog* saveFileDialog1 = new SaveFileDialog();
int saveAs(const string& outFileName)
{
string bufFile("C:\Windows\tmp.XXXXXX");
string outFile(saveFileDialog1->NewFileName);
string line;
string val;
ifstream buf_stream;
ofstream out_stream;
buf_stream.open(bufFile.c_str());
out_stream.open(outFile.c_str());
if (buf_stream)
{
while (!buf_stream.eof())
{
getline(buf_stream, line);
buf_stream >> val;
out_stream << val<<endl;
}
}
buf_stream.close();
out_stream.close();
remove("C:\Windows\tmp.XXXXXX");
return 0;
}
然后当我想保存结果时,我尝试使用该构造:
case IDM_FILE_SAVE:
{
saveFileDialog1;
saveFileDialog1->ShowDialog();
saveFileDialog1->FilterIndex1 = 1;
saveFileDialog1->Flags1 |= OFN_SHOWHELP;
saveFileDialog1->InitialDir1 = _T("C:\Windows\");
saveFileDialog1->Title1 = _T("Save File");
int retval = saveAs(saveFileDialog1->NewFileName);
}
这是我解决问题的尝试
SaveFileDialog::SaveFileDialog(void)
{
this->DefaultExtension1 = 0;
this->NewFileName = new TCHAR[MAX_PATH + TCHAR(".txt")];
this->FilterIndex1 = 1;
this->Flags1 = OFN_OVERWRITEPROMPT;
this->InitialDir1 = 0;
this->Owner1 = 0;
this->Title1 = 0;
this->RestoreDirectory = true;
}
bool SaveFileDialog::ShowDialog()
{
OPENFILENAME ofn;
TCHAR szFile[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = this->Owner1;
ofn.lpstrDefExt = this->DefaultExtension1;
ofn.lpstrFile = this->NewFileName;
ofn.lpstrFile[0] = '[=12=]';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = _T("All Files[=12=]*.*[=12=]Text files[=12=]*.txt");
ofn.nFilterIndex = this->FilterIndex1;
ofn.lpstrInitialDir = this->InitialDir1;
ofn.lpstrTitle = this->Title1;
ofn.Flags = this->Flags1;
GetSaveFileName(&ofn);
if (_tcslen(this->NewFileName) == 0) return false;
return true;
}
如有任何建议,我们将不胜感激。
使用 c++11(检查您的编译器标志?)字符串上的 fstream::open()
function works perfectly well with strings. So no need to pass through c_str()
. Then you can use the operator+
如您所愿:
buf_stream.open(bufFile);
out_stream.open(outFile);
...
outfile.open(filename + ".txt");
备注:如果在声明时文件名已知,您可以立即将文件名提供给流的构造函数。因此不需要调用单独的 open()
.
如果不能使用C++11,则使用临时字符串进行连接:
outfile.open (string(filename+".tst").c_str());