12文件读写

转自http://blog.sina.com.cn/s/articlelist_1815328704_0_1.html

1.常量指针与指针常量的区分

  char ch[5]="lisi";
  const char *pStr=ch;//const在*之前,表明指针指向的内容为常量,即为常量指针
  char * const pStr=ch;//const在*之后,表明指针的地址不能改变,即为指针常量
  明白?
2.对文件读写的三种方法
  1.C中
    FILE *pFile=fopen("1.txt","w");
fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
//fseek(pFile,0,SEEK_SET);
//fwrite("ftp:",1,strlen("ftp:"),pFile);
//fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
fclose(pFile);*/
//fflush(pFile);


  2.C++中
 要包括头文件 "fstream.h"


  3.MFC中 用CFile类,哈哈!简单好用
CFileDialog fileDlg(FALSE);
fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
fileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==fileDlg.DoModal())
{
  CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
  file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
  file.Close();
}
  4.利用win32 API函数 CreateFile(),及WriteFile()


4.注册表读写
  1.对win.ini的读写
//::WriteProfileString("http://www.sunxin.org","admin","zhangsan");


  2.注册表的读写
HKEY hKey;
DWORD dwAge=30;
RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",&hKey);
RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));
RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);
RegCloseKey(hKey);以上是写入
代码比较简单,不再详细介绍。本笔记也不是为介绍函数而存在的。嘿嘿
       // TODO: Add your command handler code here
 
       //fflush(pFile);
 
 
void CFileView::OnFileRead()
{
       // TODO: Add your command handler code here


       CFileDialog fileDlg(TRUE);
       fileDlg.m_ofn.lpstrTitle="我的文件打开对话框";
       fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
      
       if(IDOK==fileDlg.DoModal())
       {
              CFile file(fileDlg.GetFileName(),CFile::modeRead);
              char *pBuf;
              DWORD dwFileLen;
              dwFileLen=file.GetLength();
              pBuf=new char[dwFileLen+1];
              pBuf[dwFileLen]=0;
              file.Read(pBuf,dwFileLen);
              file.Close();
              MessageBox(pBuf);
       }
}
 
void CFileView::OnRegWrite()
{
       // TODO: Add your command handler code here
       HKEY hKey;
       DWORD dwAge=30;
       RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",&hKey);
       RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));
       RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);
       RegCloseKey(hKey);
}
 
void CFileView::OnRegRead()
{
       // TODO: Add your command handler code here
 
       HKEY hKey;
       RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",&hKey);
       DWORD dwType;
       DWORD dwValue;
       DWORD dwAge;
       RegQueryValueEx(hKey,"age",0,&dwType,(LPBYTE)&dwAge,&dwValue);
       CString str;
       str.Format("age=%d",dwAge);
       MessageBox(str);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值