孙鑫VC++视频学习笔记之12:文件操作

来源:互联网 发布:域名批量查询 编辑:程序博客网 时间:2024/06/05 15:54

转自:http://webbery.tianyablog.com

  阅读本文前,我们假设您已经:
   1,知道如何创建一个单文档的App Wizard
   2,知道C++ 类、函数重载等简单知识
   3,知道如何给View类或者Doc文档添加成员变量
   4,会用MFC的IDE调试工具最好,那么本文的程序您可以copy去调试
   5,知道如何为某个框架类添加虚函数或消息处理函数
  
  
  1, 指向常量的指针&&指针常量
  
  Char ch[5]=”lisi”;
  Const char * pStr=ch; const char *等同char const *
  Char * const *pStr=ch; 指针是常量,指针不可更改,其内容可更改
  
  2, 读写
  
  文件读取操作
   FILE *pFile=fopen("1.txt","r");
  
   char ch[100]="0"; //数组被赋值为全零
  memset(ch,0,100);//等同于上一句?
  
   //char ch[100]; //如果不把数组赋零,也可以在写入文件中多写一个空字符
   如fwrite("I Love You",1,strlen("I Love You")+1,pFile);
   //memset(ch,0,100); 把数组赋为全零的另一种方法。
   fread(ch,1,100,pFile);
   fflush(pFile);
  
  3, 获取文件大小
  
  fseek(pFile,0,SEEK_END); //把文件指针移到文件末尾
   int n=ftell(pFile); //得到文件长度
  
  rewind(pFile); //把指针移回文件头 fseek(pFile,0,SEEK_BEGIN)
   pbuf=new char[n+1];
   pbuf[n]=0; //文件尾加/0作为文件结束符
   fread(pbuf,1,n,pFile);
  
  4, 文本和二进制方式。读取和写入的保持一致
  文本:写入, 换行(10)è回车--换行(ASCII为13、10)
   读取, 回车--换行(ASCII 13、10)è换行(10)
  
  二进制:将数据在内存中的存储形式原样输出到文件中
  不管是文本文件还是二进制文件,都可以用文本方式或者二进制方式中的任意一种打开
  
  5, 字符和数字
  
  FILE *pFile=fopen("2.txt","w");
  
  int i=98341; //非要使他可用,可增加itoa(i,ch,10);
  fwrite(&i,4,1,pFile);
  
  6, C++中文件操作
  
  需要加头文件#include "fstream.h"
   ofstream os("3.txt");
   os.write("I love you!",strlen("I love you!"));
   os.close();
  
  读文件:
  
   ifstream ifs("3.txt",ios::in);
   char ch[100]="0";
  memset(ch,0,100)
   ifs.read(ch,100);
   ifs.close();
  
  //循环读取文件每一行
  
  While(!ifs.getline(ch,100).eof())
  
  {//do something with data in buffer ch
  
  }
  下一次重新getline之前,需要 ifs.clear()清除eof标志
  
  7, Win32API函数存取文件
  
  (1)写入
  HANDLE hfile;
   hfile=CreateFile("6.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
   DWORD dwWrites;
   WriteFile(hfile,"I Love You!(5)",strlen("I Love You!(5)"),&dwWrites,NULL);
   CloseHandle(hfile);
  (2)读取
   HANDLE hfile;
   hfile=CreateFile("6.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
   char ch[100]="0"; //如果只写char ch[dwRead-1];可以在之后用ch[dwRead]=0设结束符
   DWORD dwRead;
   ReadFile(hfile,ch,100,&dwRead,NULL);
  ch[dwRead]=0;
   CloseHandle(hfile);
  
  8, SDK方法
  
  (1)写入:
  
  CFile file("7.txt",CFile::modeCreate | CFile::modeWrite);
   file.Write("I Love You 1000",strlen("I Love You 1000"));
   file.Close();
  
  (2)读取:
  
  CFile file("7.txt",CFile::modeRead);
   char *pBuf;
   DWORD i=file.GetLength();
  
  pBuf=new char[i+1];
   file.Read(pBuf,i);
  
  pBuf[i]=0;
  
   file.Close();
  
  9, 构造文件对话框,存取文件方法
  
  (1)写入:
  
  CFileDialog fileDlg(FALSE);
   fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
   fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
  //注意lpstrFilter的构造:每个段落后边都要加/0,末尾要加两个/0,括号里的只是显示,实//现在紧跟着的/0后边,此过滤器只为过滤可见文件使用,并不能按所见格式保存。
   fileDlg.m_ofn.lpstrDefExt="txt";
   if(IDOK==fileDlg.DoModal())
   {
   CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
   file.Write("I Love You 1000",strlen("I Love You 1000"));
   file.Close();
   }
  
  (2)读取
  
   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 i=file.GetLength();
   pbuf=new char[i+1]; //动态建立缓冲区,值得学习
   pbuf[i]=0;
   file.Read(pbuf,i);
   MessageBox(pbuf);
   file.Close();
  
  }
  
  10, 读写配置文件
  
  CXXXApp::InitInstance(){
  
  // 写在SetRegistryKey(_T("Local AppWizard-Generated Applications"));之后(也可以重新设置表项)
  
  ::WriteProfileString("songpeng","sp","song");用来在C:/WINDOWS/win.ini中写入数据。一方面为了兼容十六位程序,另一方面提高程序运行速度
   //在win32中为[HKEY_CURRENT_USER]è[Software]è[Local Appwizard-Generated Applications]è[File]
  
   CString str;
   ::GetProfileString("songpeng","sp","peng",str.GetBuffer(100),100);
  
  }
  
  11, 读写注册表
  
   读写配置文件的函数WriteProfileString(),GetProfileString()在win32下自动成为注册表的读写。
  
   SetRegistryKey(_T("Local AppWizard-GeneratedApplications"));用来在注册表的HKEY_CURRENT_USER->Software下增加主键LocalAppWizard-Generated Applications
  子键及值由WriteProfileString("songpeng","sp","song");增加
  
   (1)写入注册表:
  
   HKEY hkey;
   RegCreateKey(HKEY_LOCAL_MACHINE,"Software//MyItem//Name",&hkey);RegSetValue(hkey,NULL,REG_SZ,"song",strlen("song"));
   RegSetValueEx(hKey,”age”,0,REG_DWORD,(CONST BYTE*)&dwAge,4);
  
   RegCloseKey(hkey);
  
   (2)读取注册表
  
   注意要先获取字段大小
  
   LONG lValue;
   RegQueryValue(HKEY_LOCAL_MACHINE,"Software//MyItem//Name",NULL,&lValue);
   char *buf=new char[lValue]; //注意建立缓冲区方法
   RegQueryValue(HKEY_LOCAL_MACHINE,"Software// MyItem//Name",buf,&lValue);
  
  LONG RegQueryValue(
   HKEY hKey, // handle to key to query
   LPCTSTR lpSubKey, // name of subkey to query
   LPTSTR lpValue, // buffer for returned string
   PLONG lpcbValue // receives size of returned string
  );
  If lpValue is NULL, and lpcbValue is non-NULL, the function returnsERROR_SUCCESS, and stores the size of the data, in bytes, in thevariable pointed to by lpcbValue. This lets an application determinethe best way to allocate a buffer for the value's data.所以,我们要调用两次RegQueryValue,第一次查询键值长度,第二次获得键值
  
  
  
  HKEY hkey;
   RegOpenKey(HKEY_LOCAL_MACHINE, "Software//MyItem//Name",&hkey);
   //打开主键
   DWORD dwType;
   DWORD dwAge;
   DWORD dwValue;
   RegQueryValueEx(hkey,"age",0,&dwType,(LPBYTE)&dwAge,&dwValue);
  
  其他函数:
  
   RegDeleteKey(); RegDeleteValue();等

原创粉丝点击