windows程式設計中檔案操作各種方法

2021-06-26 00:19:12 字數 3860 閱讀 3847

windows程式設計中檔案操作有以下幾種常見方法:

1.c語言中檔案操作。

2.c++語言中的檔案操作。

3.win32 api函式檔案操作。

4.mfc cfile類檔案操作。

5.mfc cfiledialog類的檔案操作。

6.登錄檔檔案操作。

下面我來詳細說明一下各種檔案操作方法:

1. c語言中檔案操作.需要包含的標頭檔案stdio.h

c++**

寫入檔案:   

file *pfile=fopen("c.txt","w");//以寫的方式開啟c.txt檔案。   

fwrite("welcome to vcfans!",1,strlen("welcome to vcfans!"),pfile);//將資料寫入檔案。   

fflush(pfile);//重新整理緩衝區。將緩衝區資料寫入檔案   

fclose(pfile);//關閉檔案   

讀取檔案:   

file *pfile=fopen("c.txt","r");//以讀的方式開啟c.txt檔案。   

char filecontent[100];   

memset(filecontent,0,100);//初始化filecontent   

fread(filecontent,1,100,pfile);//將剛才c.txt檔案中的內容讀入到filecontent   

messagebox(filecontent);//輸出結果   

fclose(pfile);//關閉檔案   

2.c++語言中的檔案操作。需要包含的標頭檔案fstream.h

c++**

寫入檔案:   

ofstream ofs("c++.txt");//建立ofstream對像。   

ofs.write("welcome to vcfans!",strlen("welcome to vcfans!"));//將資料寫入檔案   

ofs.close();//關閉ofstream物件。   

讀取檔案:   

ifstream ifs("c++.txt");   

char filecontent[100];   

memset(filecontent,0,100);//初始化filecontent   

ifs.read(filecontent,100);//讀取資料   

ifs.close();//關閉ifstream對像   

messagebox(filecontent);//輸出結果   

3.win32 api函式檔案操作。需要包含的標頭檔案winbase.h,需要類庫:kernel32.lib

c++**

寫入檔案:   

handle hfile;//定義乙個控制代碼。   

hfile=createfile("api.txt",   

generic_write,   

file_share_write,   

null,   

create_new,   

file_attribute_normal,   

null);//使用creatfile這個api函式開啟檔案   

dword written;   

writefile(hfile,"welcome to vcfans!",strlen("welcome to vcfans!"),&written,null);//寫入檔案   

closehandle(hfile);//關閉控制代碼   

讀取檔案:   

handle hfile;//定義乙個控制代碼。   

hfile=createfile("api.txt",   

generic_read,   

file_share_read,   

null,   

open_existing,   

file_attribute_normal,   

null);//使用creatfile這個api函式開啟檔案   

dword dwdatalen;   

char filecontent[100];   

readfile(hfile,filecontent,100,&dwdatalen,null);//讀取資料   

filecontent[dwdatalen]=0;//將陣列未尾設零。   

closehandle(hfile);//關閉控制代碼   

messagebox(filecontent);//輸出結果   

4.mfc cfile類檔案操作。需要包含的標頭檔案afx.h

c++**

寫入檔案:   

cfile file("cfile.txt",cfile::modecreate| cfile::modewrite);//構造cfile物件   

file.write("welcome to vcfans !",strlen("welcome to vcfans !"));//寫入資料到檔案   

file.close();//關閉cfile物件。   

讀取檔案:   

cfile file("cfile.txt",cfile::moderead);//構造cfile物件   

char filecontent[100];   

memset(filecontent,0,100);//初始化filecontent   

file.read(filecontent,100);//讀入資料   

file.close();//關閉檔案物件   

messagebox(filecontent);//輸出資料   

5.mfc cfiledialog類的檔案操作。需要包含的標頭檔案afxdlgs.h

c++**

寫入檔案:   

cfiledialog filedlg(false,"txt","cfiledialog.txt");//建立cfiledialog物件   

if(idok==filedlg.domodal())   

;   

讀取檔案:   

cfiledialog filedlg(true,"txt","cfiledialog.txt");//建立cfiledialog物件   

if(idok==filedlg.domodal())   

;   

6.登錄檔檔案操作。

c++**

寫入登錄檔:   

hkey hkey;   

dword dw***=1;   

regcreatekey(hkey_local_machine,"software\\vcfans\\reg",&hkey);//開啟登錄檔鍵   

regsetvalueex(hkey,"***",0,reg_dword,(const byte*)&dw***,4);//寫入登錄檔資料   

regclosekey(hkey);//關閉登錄檔鍵   

讀登錄檔:   

hkey hkey;   

regopenkey(hkey_local_machine,"software\\vcfans\\reg",&hkey);//開啟登錄檔鍵   

dword dwtype;   

dword dwvalue;   

dword dw***;   

regqueryvalueex(hkey,"***",0,&dwtype,(lpbyte)&dw***,&dwvalue);//查詢登錄檔資料   

regclosekey(hkey);//關閉登錄檔鍵   

cstring str;   

str.format("***=%d",dw***);   

messagebox(str);   

python中檔案操作各符號意思

我以python中open函式為例 r 開啟唯讀檔案,該檔案必須存在。r 開啟可讀寫的檔案,該檔案必須存在。w 開啟只寫檔案,若檔案存在則檔案長度清為0,即該檔案內容會消失。若檔案不存在則建立該檔案。之後可能不能用python 刪除檔案會出現以下錯誤 如果要對檔案進行更深一步的操作則需用w w 開啟...

Windows程式設計中檔案操作有以下幾種常見方法

1.c語言中檔案操作。2.c 語言中的檔案操作。3.win32 api函式檔案操作。4.mfc cfile類檔案操作。5.mfc cfiledialog類的檔案操作。6.登錄檔檔案操作。下面我來詳細說明一下各種檔案操作方法 1.c語言中檔案操作.需要包含的標頭檔案stdio.h 寫入檔案 file ...

Windows 檔案 目錄操作程式設計常用API

1 檔案操作函式 createfile 建立或開啟檔案 writefile 寫資料到檔案 readfile 從檔案讀資料 copyfile 拷貝檔案 movefile 移動或重新命名檔案 deletefile 刪除檔案 getmodulefilename 獲取檔案目錄 setfilepointer ...