魔兽世界月卡兑换:孙鑫VC视频教程笔记之第十二课“文件操作(含注册表操作)”

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 15:33:12
 

文件读写:

C语言实现:

      文件读

      //方法1:

      FILE *pFile=fopen("1.txt","r");

      char content[20];

      memset(content,0,20); //将数组中的内容全部设置为\0,下一行语句中遇到\0就不读取了

      fread(content,1,20,pFile); //读取文件内容,直到遇见\0字符

 

      //方法2:根据文件大小动态声明数组大小

      char *content;

      fseek(pFile,0,SEEK_END); //将文件指针移动到末尾

      long size=ftell(pFile);

      //fseek(pFile,0,SEEK_SET); //再此将指针移动到开始位置,从而读取

      rewind(pFile); //用途和上一行语句一样

      content=new char[size+1]; //多一个用来存放结尾操作符

      fread(content,1,size,pFile);

      content[size]=0;

      MessageBox(content);

      fclose(pFile);

     

      文件写:

       //如果缺少fflush或这fclose函数,程序exe不关闭的话,则输出内容不会从缓冲区中输出到文件中

      FILE *pFile=fopen("1.txt","w"); //以读的方式打开

      char test[3];

      test[0]='a';

      test[1]=10; //换行

      test[2]='b';

      fwrite(test,1,3,pFile); //将输出文件用Utraledit的16机制打开,发现中间多了一个回车0D(13)

      //fwrite("hello world",1,strlen("hello world"),pFile);

      fseek(pFile,0,SEEK_SET); //将文件指针移动到文件开始位置,用来跳转写入

      fflush(pFile); //系统会将缓冲区中的内容立 即写入到文件中,无需fclose操作也能成功保存

      fclose(pFile); //关闭文件

      

       思考题:将数字12345写到到文本文件中

FILE *pFile=fopen("1.txt","w");

      char test[5];

      //0的ASSII码是48,因为在文本文件中存放的都是字符,所以必须得到指定数字对应的字符

      test[0]=1+48;

      test[1]=2+48;

      test[2]=3+48;

      test[3]=4+48;

      test[4]=5+48;

      fwrite(test,1,5,pFile);

 

      //也可以按以下方式实现:

      int test=12345;

      char *str;

      itoa(test,str,10);

      fwrite(str,1,5,pFile);

 

C++语言实现:

      文件读:

       ifstream ifs("1.txt");

      char ch[100];

      memset(ch,0,100);

      ifs.read(ch,100);

      ifs.close();

      MessageBox(ch);

 

       文件写:

       ofstream os("1.txt");

      os.write("hello world",strlen("hello world"));

      os.close();

 

Win 32 API实现:

      文件读:

      HANDLE hFile=CreateFile("1.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,

            FILE_ATTRIBUTE_NORMAL,NULL);

      char result[100];

      DWORD readed; //实际读取到的字节数

      ReadFile(hFile,result,100,&readed,NULL);

      result[readed]=0; //结束字符

      CloseHandle(hFile);

      MessageBox(result);

      文件写:

       HANDLE hFile=CreateFile("1.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,

           FILE_ATTRIBUTE_NORMAL,NULL);

      DWORD writen;

      WriteFile(hFile,"hello world",strlen("hello world"),&writen,NULL);

      CloseHandle(hFile);

 

MFC实现:

      文件读:

      CFile file("1.txt",CFile::modeRead);

      char *buffer;

      DWORD length=file.GetLength();

      buffer=new char[length+1];

      buffer[length]=0;

      file.Read(buffer,length);

      file.Close();

      MessageBox(buffer);

      文件写:

      CFile file("1.txt",CFile::modeCreate|CFile::modeWrite);

      file.Write("hello world!",strlen("hello world!"));

      file.Close();

 

文件打开保存对话框:

       文件打开对话框:

       CFileDialog dlg(TRUE);

      dlg.m_ofn.lpstrTitle="我的文件打开";

      dlg.m_ofn.lpstrFilter="Text Files(.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";

      if(IDOK==dlg.DoModal())

      {

           CFile file(dlg.GetFileName(),CFile::modeRead);

           char *buffer;

           DWORD length=file.GetLength();

           buffer=new char[length+1];

           buffer[length]=0;

           file.Read(buffer,length);

           file.Close();

           MessageBox(buffer);

      }

 

      文件保存对话框:

      CFileDialog dlg(FALSE);

      dlg.m_ofn.lpstrTitle="我的文件保存";

      dlg.m_ofn.lpstrFilter="Text Files(.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";

      dlg.m_ofn.lpstrDefExt="txt";

      if(IDOK==dlg.DoModal())

      {

           CFile file(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);

           file.Write("hello world!",strlen("hello world!"));

           file.Close();

      }

注册表读写:

       系统默认位置(在NT之前是记录在Win.ini文件中):

       读:

       CString strSection       = "My Section";

      CString strStringItem    = "My String Item";  

      CWinApp* pApp = AfxGetApp();

      CString result=pApp->GetProfileString(strSection, strStringItem, NULL);

      MessageBox(result);

       写:

       //由MSDN得知,在NT以后,WriteProfileString函数将设置写在注册表中

      CString strSection       = "My Section";

      CString strStringItem    = "My String Item";  

      CWinApp* pApp = AfxGetApp();

      pApp->WriteProfileString(strSection, strStringItem, "test");

 

       用户自定义位置:

       读:

       LONG strLenth;

      //RegQueryValue就是为了得到没有名称或默认的Value的值的

      //第一次调用为了得到值的大小

      RegQueryValue(HKEY_LOCAL_MACHINE,"software\\VS\\Test",NULL,&strLenth);

      //在MSDN中已经指出RegQueryValue函数的最后一个参数lpcbValue已经包含了终止符号,

      //所以不需要在此添加终止符号了,即char *pBuffer=new char[strLenth+1];

      char *pBuffer=new char[strLenth];

      //第二次调用,才真正是为了得到值

      RegQueryValue(HKEY_LOCAL_MACHINE,"software\\VS\\Test",pBuffer,&strLenth);

      MessageBox(pBuffer);

 

      // RegQueryValueEx 函数的使用为了获得指定名称的值

      HKEY hKey;

      DWORD dwLength; //接收值的长度

      DWORD dwType; //接收值的类型

      DWORD dwValue;  //接收值本身

      RegOpenKey(HKEY_LOCAL_MACHINE,"software\\VS\\Test",&hKey); //打开子项

      RegQueryValueEx(hKey,"age",NULL,&dwType,(LPBYTE)&dwValue,&dwLength); //打开值

      CString result;

      result.Format("%d",dwValue);

      MessageBox(result);

 

       写:

       HKEY hKey;

      RegCreateKey(HKEY_LOCAL_MACHINE,"software\\VS\\Test",&hKey);

 

      //RegSetValue就是为了给没有名称或默认的Value设置值的

      RegSetValue(hKey,NULL,REG_SZ,"HELLO WORLD",strlen("HELLO WORLD"));

      RegCloseKey(hKey);

 

      //RegSetValueEx 函数为了设置指定名称值存在,当然该函数也可以实现RegSetValue的功能

      DWORD dwAge=30;

      RegSetValueEx(hKey,"age",0,REG_DWORD,(BYTE*)&dwAge,4);

      RegCloseKey(hKey);