void CMapEditDlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
FILE *pFileout; // 文件操作指针
char caption[100] = "";
char stype[100] = "";
char texture[100] = "";
char music[100] = "";
int height = 0;
int width = 0;
int t = 0;
CString str;
// 以二进制方式打开user.dat文件
CFileDialog dlg(TRUE,"*.map",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"Map Files (*.map)|*.map|",NULL);
if (dlg.DoModal() == IDOK)
{
str = dlg.GetPathName();
}
else
{
return;
}
if ((pFileout = fopen(str, "rb")) == NULL)
{
printf("cannot open file");
exit(0);
}
// 成块从文件中读取
FILE *stream = tmpfile();//创建临时文件
//解密文件
char ch;
ch=fgetc(pFileout);
while(!feof(pFileout))
{
ch=ch^(0x100111);
fputc(ch,stream);
ch=fgetc(pFileout);
}
fclose(pFileout);
rewind(stream);//移至文件头
fread(&caption, 100, 1, stream);
fread(&stype, 100, 1, stream);
fread(&texture, 100, 1, stream);
fread(&music, 100, 1, stream);
fread(&height, sizeof(height), 1, stream);
fread(&width, sizeof(width), 1, stream);
int mymap[100];
int n = 0;
fread(&mymap, 300, 1, stream);
_rmtmp();//删除临时文件
//向页面写内容
str = caption;
m_cCaption = str;
str = stype;
m_cStype = str;
str = texture;
m_cTextTure = str;
str = music;
m_cMusic = str;
m_nWidth = width;
m_nHeight = height;
CString strmapTemp = "";
CString strmap = "";
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
strmapTemp.Format("%d",mymap[n]);
strmap += strmapTemp + ",";
n++;
}
}
m_cMap = strmap;
UpdateData(FALSE);
}
void CMapEditDlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here
// 信息提交到user.dat中
UpdateData(TRUE);
if (""==m_cCaption)
{
MessageBox("Caption empty");
return;
}
FILE *pFilein;
char caption[100];
//获取叶面内容
strcpy(caption,m_cCaption);
char stype[100];
strcpy(stype,m_cStype);
char texture[100];
strcpy(texture,m_cTextTure);
char music[100];
strcpy(music,m_cMusic);
int height = m_nHeight;
int width = m_nWidth;
int t = 0;
int mymap[100];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
mymap[t] = t;
t++;
}
}
CString str;
str = "C://"+m_cCaption+".map";
if ((pFilein = fopen(str, "ab+")) == NULL)
{
printf("cannot open file");
exit(0);
}
FILE *stream = tmpfile();//创建临时文件
fwrite(&caption, 100, 1, stream); // 将内容写入临时文件
fwrite(&stype, 100, 1, stream);
fwrite(&texture, 100, 1, stream);
fwrite(&music, 100, 1, stream);
fwrite(&height, sizeof(height), 1, stream);
fwrite(&width, sizeof(width), 1, stream);
fwrite(&mymap, 300, 1, stream);
rewind(stream);//移至文件头
//加密文件
char ch;
ch=fgetc(stream);
while(!feof(stream))
{
ch=ch^(0x100111);
fputc(ch,pFilein);
ch=fgetc(stream);
}
_rmtmp();//删除临时文件
fclose(pFilein);
}
加密码还可如下使用,其中感觉 密码也需要加密
char ch;
ch=fgetc(stream);
CString s = _T("passwordfsdfasdfasdf");
char *psw = s.GetBuffer(0);
int j= i = 0;
while(psw[++i]);
while(!feof(stream))
{
ch=ch^psw[j>=i?j=0:j++];
fputc(ch,pFilein);
ch=fgetc(stream);
}
本文介绍了一个地图编辑对话框中实现的地图文件(.map)读写功能及加密过程。涉及文件选择对话框的使用、文件的二进制读写、简单加密解密算法的应用等关键技术点。
1617

被折叠的 条评论
为什么被折叠?



