【发布时间】:2015-02-27 12:28:58
【问题描述】:
问题是我需要在保存文件名时添加扩展名。像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] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = _T("All Files\0*.*\0Text files\0*.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;
}
任何建议将不胜感激。
【问题讨论】:
-
为什么
outfile.open(filename + ".txt")不适合你? -
当我输入
out_stream.open(outFile.c_str() + ".txt");时,VS 会返回错误:1 IntelliSense: expression must have integer or unscoped enum type -
@RuslanMuhamadiarov 您正在尝试添加两个指针。省略“.c_str()”。
-
谢谢哥们!我很乐意为您的评论投票,但很抱歉我不能因为代表太低^^'
标签: c++ winapi file-extension savefiledialog