无题

本文介绍了一个地图编辑对话框中实现的地图文件(.map)读写功能及加密过程。涉及文件选择对话框的使用、文件的二进制读写、简单加密解密算法的应用等关键技术点。

 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);
 }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值