MFC中CFileDialog只能覆盖保存读取但是却不能直接保存是什么原因?
voidCFiveInLineView::OnSave(){//TODO:Addyourcommandhandlercodehere//设置保存的文件,后缀名wzqCFi...
void CFiveInLineView::OnSave()
{
// TODO: Add your command handler code here
//设置保存的文件,后缀名wzq
CFileDialog dlg(FALSE, "wzq", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "(*.WZQ)|*.wzq|All Files|*.*||", this);
//如果公共类对话框为确定
if (dlg.DoModal() == IDOK)
//获取文件名
dlg.GetFileName();
//否则,退出
else
return;
//字符串变量
CString str;
int i, j;
CStdioFile file;
//如果有问题,退出
if (file.Open(dlg.GetFileName(), CFile::modeCreate | CFile::modeWrite | CFile::typeText) == 0)
{
AfxMessageBox("save error!");
return;
}
//循环把棋盘数组的值写进文件
for (i = 0; i<16; i++)
for (j = 0; j<16; j++)
{
if (wzq[i][j] == -1)
file.WriteString("-1\n");
if (wzq[i][j] == 0)
file.WriteString("0\n");
if (wzq[i][j] == 1)
file.WriteString("1\n");
}
//保存当前下棋颜色
if (colorwhite == true)
file.WriteString("1\n");
else
file.WriteString("0\n");
//关闭文件
file.Close();
}
//与保存文件相反
void CFiveInLineView::OnOpen()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE, "wzq", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "(*.WZQ)|*.wzq|All Files|*.*||", this);
if (dlg.DoModal() == IDOK)
dlg.GetFileName();
else
return;
CString str;
int i, j, m;
CStdioFile file;
if (file.Open(dlg.GetFileName(), CFile::modeRead) == 0)
{
AfxMessageBox("read error!");
return;
}
CArchive ar(&file, CArchive::load);
for (i = 0; i<16; i++)
for (j = 0; j<16; j++)
{
ar.ReadString(str);
sscanf_s(str, "%d", &m);
if (m == -1)
wzq[i][j] = -1;
if (m == 0)
wzq[i][j] = 0;
if (m == 1)
wzq[i][j] = 1;
}
ar.ReadString(str);
sscanf_s(str, "%d", &m);
if (m == 1)
colorwhite = true;
else
colorwhite = false;
file.Close();
ar.Close();
Invalidate(false);
} 展开
{
// TODO: Add your command handler code here
//设置保存的文件,后缀名wzq
CFileDialog dlg(FALSE, "wzq", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "(*.WZQ)|*.wzq|All Files|*.*||", this);
//如果公共类对话框为确定
if (dlg.DoModal() == IDOK)
//获取文件名
dlg.GetFileName();
//否则,退出
else
return;
//字符串变量
CString str;
int i, j;
CStdioFile file;
//如果有问题,退出
if (file.Open(dlg.GetFileName(), CFile::modeCreate | CFile::modeWrite | CFile::typeText) == 0)
{
AfxMessageBox("save error!");
return;
}
//循环把棋盘数组的值写进文件
for (i = 0; i<16; i++)
for (j = 0; j<16; j++)
{
if (wzq[i][j] == -1)
file.WriteString("-1\n");
if (wzq[i][j] == 0)
file.WriteString("0\n");
if (wzq[i][j] == 1)
file.WriteString("1\n");
}
//保存当前下棋颜色
if (colorwhite == true)
file.WriteString("1\n");
else
file.WriteString("0\n");
//关闭文件
file.Close();
}
//与保存文件相反
void CFiveInLineView::OnOpen()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE, "wzq", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "(*.WZQ)|*.wzq|All Files|*.*||", this);
if (dlg.DoModal() == IDOK)
dlg.GetFileName();
else
return;
CString str;
int i, j, m;
CStdioFile file;
if (file.Open(dlg.GetFileName(), CFile::modeRead) == 0)
{
AfxMessageBox("read error!");
return;
}
CArchive ar(&file, CArchive::load);
for (i = 0; i<16; i++)
for (j = 0; j<16; j++)
{
ar.ReadString(str);
sscanf_s(str, "%d", &m);
if (m == -1)
wzq[i][j] = -1;
if (m == 0)
wzq[i][j] = 0;
if (m == 1)
wzq[i][j] = 1;
}
ar.ReadString(str);
sscanf_s(str, "%d", &m);
if (m == 1)
colorwhite = true;
else
colorwhite = false;
file.Close();
ar.Close();
Invalidate(false);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询