送出100分!关于文件保存的问题,急!

zgjszj 2005-07-13 09:57:48
我在多文档界面中定义了一个文件保存函数,代码如下:
void CControlView::OnFileSave2()
{
CFileDialog dlg(FALSE,"",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"控制文件(*.*)|*.*|",NULL);
if(dlg.DoModal()==IDOK)
{
CCONTROLDoc * pDoc=GetDocument();
UpdateData(TRUE);
CFile f;
VERIFY(f.Open(dlg.GetPathName(),CFile::modeCreate|CFile::modeWrite));
CArchive ar(&f,CArchive::store);
GetDlgItem(IDC_EDIT1)->GetWindowText(constr[0]);
constr[0]="$PROBLEM "+constr[0]+"\r\n";
ar.WriteString(constr[0]);
constr[1]="$INPUT";
if(m_idchk==TRUE)
constr[1]=constr[1]+" ID";
if(m_timechk==TRUE)
constr[1]=constr[1]+" TIME";
if(m_amtchk==TRUE)
constr[1]=constr[1]+" AMT";
if(m_wtchk==TRUE)
constr[1]=constr[1]+" WT";
if(m_apgrchk==TRUE)
constr[1]=constr[1]+" APGR";
if(m_dvchk==TRUE)
constr[1]=constr[1]+" DV";
constr[1]=constr[1]+"\r\n";
ar.WriteString(constr[1]);
pDoc->SetPathName(dlg.GetPathName());
ar.Close();
f.Close();
}
}

点击菜单中的"保存"能正常保存文件,但有一点问题:当我打开一个已保存的文件例如cf,修改后再点"保存",程序又弹出保存对话框,如果我输入文件名cf1,还是能成功保存.但是按道理说不应该再弹出保存对话框的,应该直接保存到文件cf1中.请问各位大侠,该如何解决这个问题?
...全文
107 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qrlvls 2005-07-13
  • 打赏
  • 举报
回复
现在参考一下 MFC 自身的处理过程:
BOOL CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
// Save the document data to a file
// lpszPathName = path name where to save document file
// if lpszPathName is NULL then the user will be prompted (SaveAs)
// note: lpszPathName can be different than 'm_strPathName'
// if 'bReplace' is TRUE will change file name if successful (SaveAs)
// if 'bReplace' is FALSE will not change path name (SaveCopyAs)
{
CString newName = lpszPathName;
if (newName.IsEmpty())
{
CDocTemplate* pTemplate = GetDocTemplate();
ASSERT(pTemplate != NULL);

newName = m_strPathName;
if (bReplace && newName.IsEmpty())
{
newName = m_strTitle;
// check for dubious filename
int iBad = newName.FindOneOf(_T(" #%;/\\"));
if (iBad != -1)
newName.ReleaseBuffer(iBad);

// append the default suffix if there is one
CString strExt;
if (pTemplate->GetDocString(strExt, CDocTemplate::filterExt) &&
!strExt.IsEmpty())
{
ASSERT(strExt[0] == '.');
newName += strExt;
}
}

if (!AfxGetApp()->DoPromptFileName(newName,
bReplace ? AFX_IDS_SAVEFILE : AFX_IDS_SAVEFILECOPY,
OFN_HIDEREADONLY | OFN_PATHMUSTEXIST, FALSE, pTemplate))
return FALSE; // don't even attempt to save
}

CWaitCursor wait;

if (!OnSaveDocument(newName))
{
if (lpszPathName == NULL)
{
// be sure to delete the file
TRY
{
CFile::Remove(newName);
}
CATCH_ALL(e)
{
TRACE0("Warning: failed to delete file after failed SaveAs.\n");
DELETE_EXCEPTION(e);
}
END_CATCH_ALL
}
return FALSE;
}

// reset the title and change the document name
if (bReplace)
SetPathName(newName);

return TRUE; // success
}
laolaoliu2002 2005-07-13
  • 打赏
  • 举报
回复
把OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT改掉具体的可以看MSDN.
qrlvls 2005-07-13
  • 打赏
  • 举报
回复
现在参考一下 MFC 自身的处理过程:
BOOL CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
// Save the document data to a file
// lpszPathName = path name where to save document file
// if lpszPathName is NULL then the user will be prompted (SaveAs)
// note: lpszPathName can be different than 'm_strPathName'
// if 'bReplace' is TRUE will change file name if successful (SaveAs)
// if 'bReplace' is FALSE will not change path name (SaveCopyAs)
{
CString newName = lpszPathName;
if (newName.IsEmpty())
{
CDocTemplate* pTemplate = GetDocTemplate();
ASSERT(pTemplate != NULL);

newName = m_strPathName;
if (bReplace && newName.IsEmpty())
{
newName = m_strTitle;
// check for dubious filename
int iBad = newName.FindOneOf(_T(" #%;/\\"));
if (iBad != -1)
newName.ReleaseBuffer(iBad);

// append the default suffix if there is one
CString strExt;
if (pTemplate->GetDocString(strExt, CDocTemplate::filterExt) &&
!strExt.IsEmpty())
{
ASSERT(strExt[0] == '.');
newName += strExt;
}
}

if (!AfxGetApp()->DoPromptFileName(newName,
bReplace ? AFX_IDS_SAVEFILE : AFX_IDS_SAVEFILECOPY,
OFN_HIDEREADONLY | OFN_PATHMUSTEXIST, FALSE, pTemplate))
return FALSE; // don't even attempt to save
}

CWaitCursor wait;

if (!OnSaveDocument(newName))
{
if (lpszPathName == NULL)
{
// be sure to delete the file
TRY
{
CFile::Remove(newName);
}
CATCH_ALL(e)
{
TRACE0("Warning: failed to delete file after failed SaveAs.\n");
DELETE_EXCEPTION(e);
}
END_CATCH_ALL
}
return FALSE;
}

// reset the title and change the document name
if (bReplace)
SetPathName(newName);

return TRUE; // success
}
ihavenoidea 2005-07-13
  • 打赏
  • 举报
回复
在 文档类里~ 序列化你的数据
qrlvls 2005-07-13
  • 打赏
  • 举报
回复
CDocument *pDoc;

strFile = pDoc->GetPathName();
if (strFile == _T(""))
{
弹出文件对话框
strFile = dlgFileOpen.GetPathName();
}

CCONTROLDoc * pDoc=GetDocument();
UpdateData(TRUE);
CFile f;
VERIFY(f.Open(dlg.GetPathName(),CFile::modeCreate|CFile::modeWrite));
CArchive ar(&f,CArchive::store);
GetDlgItem(IDC_EDIT1)->GetWindowText(constr[0]);
constr[0]="$PROBLEM "+constr[0]+"\r\n";
ar.WriteString(constr[0]);
constr[1]="$INPUT";
if(m_idchk==TRUE)
constr[1]=constr[1]+" ID";
if(m_timechk==TRUE)
constr[1]=constr[1]+" TIME";
if(m_amtchk==TRUE)
constr[1]=constr[1]+" AMT";
if(m_wtchk==TRUE)
constr[1]=constr[1]+" WT";
if(m_apgrchk==TRUE)
constr[1]=constr[1]+" APGR";
if(m_dvchk==TRUE)
constr[1]=constr[1]+" DV";
constr[1]=constr[1]+"\r\n";
ar.WriteString(constr[1]);
pDoc->SetPathName(dlg.GetPathName());
ar.Close();
f.Close();
}
qrlvls 2005-07-13
  • 打赏
  • 举报
回复
你使用的是自定义的保存函数,与默认的框架结构中的函数的处理过程是不同的
ihavenoidea 2005-07-13
  • 打赏
  • 举报
回复
用序列化~

你这样当然是每次都打开了~
一条晚起的虫 2005-07-13
  • 打赏
  • 举报
回复
改成 OFN_HIDEREADONLY | ~OFN_OVERWRITEPROMPT
一条晚起的虫 2005-07-13
  • 打赏
  • 举报
回复
OFN_OVERWRITEPROMPT
Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.

你在建CFileDialog 的时候,选的参数就是这样的
dasiu 2005-07-13
  • 打赏
  • 举报
回复
自动保存到制定位置,需要你在程序里面处理。
例如,在第一次弹出对话框保存后,利用变量保存保存文件路径和名称;
在再次按下保存的时候,利用原来的路径和名称,直接保存而不弹出对话框。
goodboyws 2005-07-13
  • 打赏
  • 举报
回复
改成这样
CString strPath = GetDocument()->GetPathName();
if (strPath == "")
{
CFileDialog dlg(FALSE,"",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"控制文件(*.*)|*.*|",NULL);
if(dlg.DoModal()==IDOK)
{
strPath == dlg.GetPathName();
pDoc->SetPathName(dlg.GetPathName());
}
}
if (strPath != "")
{
CCONTROLDoc * pDoc=GetDocument();
UpdateData(TRUE);
CFile f;
VERIFY(f.Open(strPath ,CFile::modeCreate|CFile::modeWrite));
CArchive ar(&f,CArchive::store);
GetDlgItem(IDC_EDIT1)->GetWindowText(constr[0]);
constr[0]="$PROBLEM "+constr[0]+"\r\n";
ar.WriteString(constr[0]);
constr[1]="$INPUT";
if(m_idchk==TRUE)
constr[1]=constr[1]+" ID";
if(m_timechk==TRUE)
constr[1]=constr[1]+" TIME";
if(m_amtchk==TRUE)
constr[1]=constr[1]+" AMT";
if(m_wtchk==TRUE)
constr[1]=constr[1]+" WT";
if(m_apgrchk==TRUE)
constr[1]=constr[1]+" APGR";
if(m_dvchk==TRUE)
constr[1]=constr[1]+" DV";
constr[1]=constr[1]+"\r\n";
ar.WriteString(constr[1]);

ar.Close();
f.Close();

}
酷窗版演示:http://www.edd8.com/bbs/index.asp?style=0 简装版演示:http://www.edd8.com/bbs/index2.asp 一点点论坛(http://www.edd8.com/bbs)使用说明 欢迎大家下载使用一点点论坛,在使用论坛前,请认真阅读以下内容: ===================================== 论坛:一点点论坛(http://www.edd8.com/bbs) 主页:一点点星空驿站(http://www.edd8.com/) 站长:叮咚虫(e_Mail:b_li@163.com) 版本:EDD8 Ver.2003 for DV =====================================   一点点论坛是建立在动网论坛基础上的ASP互动论坛,从动网Var5.b109开始论坛结构便开始脱离动网,与动网升级一起,本论坛本身也同时实现了动网新的功能,到519的发布,本人常得论坛本身已功能基本完善,所以决定与动网完全脱离,到动网Final的出现,本论坛也决定不再跟随动网升级了,本人全面对原来的论坛进行了代码优化和版面重排,并升级了部功能代码,完成了现在这样的论坛。使论坛已经完全脱离了动网,以后也不可以随动网一起升级了!   所以选择本论坛的朋友请先明白,如果采用了本论坛的数据结构,以后就不可以和动网一起升级了,而现有的动网Final版的数据库和本论坛的数据库也是不兼容的!对于动网V5.b519的用户,本人同时在压缩包里提供了升级文件,可以把你519的数据库升级成和本论坛一致的数据结构!   由于一些朋友催得较,部功能还是没有完善的(但不会出错),我以后也会给出升级包的。所以决定使用本论坛的朋友,请一定要到本站论坛的“站务办公室”进行指定的留言签名,以好我第一时间通知大家升级!   论坛中使用到的部插件并没有一同奉上,原因是我还没完成代码优化,请过几天到我的论坛上下载。可以下载的插件主要有:社区银行、网络拳皇、可乐吧台球、五子棋等等…… ===================================== 主要特色功能说明: ◎ 双版面设计,用户可以根据不同爱好选择不同的版式(酷窗版和精简版),系统会记录用户的选择,不用重复选择,也不会出现版面混淆等现象; ◎ 发贴回贴互动功能,给用户随机的互动事件,增加用户的参与兴趣; ◎ 增强的UBB代码和JS代码,新增的买卖贴、定时贴等十几种特色功能UBB代码; ◎ 贴子功能加强,增加互动鲜花、鸡蛋、金钱、炸弹等功能,并保存数据作为用户在论坛的人缘依据; ◎ 不好说了,等你去发掘吧,不然又说我叫卖了…… ====================================== 安装说明: 解压就可以了,数据库名称和路径有变动时不要忘了在CONN。ASP中进行相应修改。 (注意:当论坛中一个贴子也没有时,论坛明星会出错,但当你加入贴子后就正常了,大家不要惊慌) 升级:(对519),解压后把你的原519数据库覆盖现数据库,再执行根目录下的UPDATE。ASP文件就行了,如果没有升级成功的提示,说明你的数据不能进行升级! (注意:数据库的升级应做了备份,并在本机上进行!) ====================================== 最后一点说明: 由于论坛本来是打算自己用的,所以在设计的过程中部变量已经去掉,不能从后台设置了(如表格边框),请大家最好保持现有的风格。 如果还不问题,可以到我的论坛上来交流。??注意,由于本人时间不限,技术支持是有限的,请不要过于强求!

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧