孫鑫mfc学習ノート第12課
第十二課
const char*とchar*constの違い.C言語のファイルの読み書きに対するサポート、FILEポインタ;テキストファイルとバイナリファイルの違い.ファイルをテキストで読み書きすることと、ファイルをバイナリで読み書きすることの注意点.C++ファイルの読み書きサポート、ofstreamとifstreamの使い方.Win 32 SDKのファイル読み書きのサポート、CreateFile関数、WriteFile関数、ReadFile関数の使用;MFCのファイル読み書きサポート、CFIleクラスとCFIleDialogの使用、ファイルフィルタの設定.win.iniファイルとレジストリの読み書き方法と関連知識点.
本文は“かつて遠く離れません”のブログから出て、転載をお断りします!
const char*とchar*constの違い.C言語のファイルの読み書きに対するサポート、FILEポインタ;テキストファイルとバイナリファイルの違い.ファイルをテキストで読み書きすることと、ファイルをバイナリで読み書きすることの注意点.C++ファイルの読み書きサポート、ofstreamとifstreamの使い方.Win 32 SDKのファイル読み書きのサポート、CreateFile関数、WriteFile関数、ReadFile関数の使用;MFCのファイル読み書きサポート、CFIleクラスとCFIleDialogの使用、ファイルフィルタの設定.win.iniファイルとレジストリの読み書き方法と関連知識点.
- Ini
-
- ::WriteProfileString("http://www.sunxin.org","admin","zhangsan"); ini,
- CString str;
- ::GetProfileString("http://www.sunxin.org","admin","lisi",str.GetBuffer(100),100); ini,
-
-
-
-
- WriteProfileString("my ini test","jiajia","i love hf very much");
- GetProfileString("my ini test","jiajia","lisi");
-
-
- 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
- /* LONG lValue;
- RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",
- NULL,&lValue);
- char *pBuf=new char[lValue];
- RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",
- pBuf,&lValue);
- MessageBox(pBuf);*/
- 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);
- }
-
- 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);
-
- /* FILE *pFile=fopen("2.txt","wb");
- char ch[3];
- ch[0]='a';
- ch[1]=10;
- ch[2]='b';
- fwrite(ch,1,3,pFile);
- fclose(pFile);*/
-
- /*FILE *pFile=fopen("3.txt","w");
- int i=98341;
- char ch[5];*/
- /*ch[0]=9+48;
- ch[1]=8+48;
- ch[2]=3+48;
- ch[3]=4+48;
- ch[4]=1+48;*/
- /*itoa(i,ch,10);
-
- //fwrite(&i,4,1,pFile);
- fwrite(ch,1,5,pFile);
- fclose(pFile);*/
- C++
- /* ofstream ofs("4.txt");
- ofs.write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
- ofs.close();*/
-
-
-
- C
- /* FILE *pFile=fopen("1.txt","r");
- // char ch[100];
- // memset(ch,0,100);
- // fread(ch,1,100,pFile);
- // MessageBox(ch);
- char *pBuf;
- fseek(pFile,0,SEEK_END);
- int len=ftell(pFile);
- pBuf=new char[len+1];
- rewind(pFile);
- fread(pBuf,1,len,pFile);
- pBuf[len]=0;
- MessageBox(pBuf);
- fclose(pFile);*/
- /* FILE *pFile=fopen("2.txt","rb");
- char ch[100];
- fread(ch,1,3,pFile);
- ch[3]=0;
- MessageBox(ch);
- fclose(pFile);*/
- C++
- /* ifstream ifs("4.txt");
- char ch[100];
- memset(ch,0,100);
- ifs.read(ch,100);
- ifs.close();
- MessageBox(ch);*/
-
-
- Windows api
- /* HANDLE hFile;
- hFile=CreateFile("5.txt",GENERIC_READ,0,NULL,OPEN_EXISTING, // ,
- FILE_ATTRIBUTE_NORMAL,NULL);
- char ch[100];
- DWORD dwReads;
- ReadFile(hFile,ch,100,&dwReads,NULL); //
- ch[dwReads]=0;
- CloseHandle(hFile);
- MessageBox(ch);*/
- /* CFile file("6.txt",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);*/
-
-
- /* HANDLE hFile;
- hFile=CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,
- FILE_ATTRIBUTE_NORMAL,NULL);
- DWORD dwWrites;
- WriteFile(hFile,"http://www.sunxin.org",strlen("http://www.sunxin.org"),//
- &dwWrites,NULL);
- CloseHandle(hFile);*/
- /* CFile file("6.txt",CFile::modeCreate | CFile::modeWrite);
- file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
- file.Close();*/
-
-
-
-
- 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();
- }
-
-
-
- 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);
- }
本文は“かつて遠く離れません”のブログから出て、転載をお断りします!