mfc下檔案的基本操作

2021-05-22 14:39:27 字數 4554 閱讀 2449

1.檔案的查詢

當對乙個檔案操作時,如果不知道該檔案是否存在,就要首先進行查詢。mfc中有乙個專門用來進行檔案查詢的類cfilefind,使用它可以方便快捷地進行檔案的查詢。下面這段**演示了這個類的最基本使用方法。

cstring strfiletitle;

cfilefind finder;

bool bworking = finder.findfile("c://windows//sysbkup//*.cab");

while(bworking)

注:(api)   findfirstfile,findnextfile

2.檔案的開啟/儲存對話方塊

讓使用者選擇檔案進行開啟和儲存操作時,就要用到檔案開啟/儲存對話方塊。mfc的類cfiledialog用於實現這種功能。使用cfiledialog宣告乙個物件時,第乙個bool型引數用於指定檔案的開啟或儲存,當為true時將構造乙個檔案開啟對話方塊,為false時構造乙個檔案儲存對話方塊。

在構造cfiledialog物件時,如果在引數中指定了ofn_allowmultiselect風格,則在此對話方塊中可以進行多選操作。此時要重點注意為此cfiledialog物件的m_ofn.lpstrfile分配一塊記憶體,用於儲存多選操作所返回的所有檔案路徑名,如果不進行分配或分配的記憶體過小就會導致操作失敗。下面這段程式演示了檔案開啟對話方塊的使用方法。

cfiledialog mfiledlg(true,null,null,

ofn_hidereadonly|ofn_overwriteprompt|ofn_allowmultiselect,

"all files (*.*)|*.*||",afxgetmainwnd());

cstring str(" ",10000);

mfiledlg.m_ofn.lpstrfile=str.getbuffer(10000);

str.releasebuffer();

position mpos=mfiledlg.getstartposition();

cstring pathname(" ",128);

cfilestatus status;

while(mpos!=null)

3.檔案的讀寫

檔案讀寫的最普通的方法是直接使用cfile進行,如檔案的讀寫可以使用下面的方法:

//對檔案進行讀操作

char sread[2];

cfile mfile(_t("user.txt"),cfile::moderead);

if(mfile.getlength()<2) return;

mfile.read(sread,2);

mfile.close();

//對檔案進行寫操作

cfile mfile(_t("user.txt "), cfile::modewrite|cfile::modecreate);

mfile.write(sread,2);

mfile.flush();

mfile.close();

雖然這種方法最為基本,但是它的使用繁瑣,而且功能非常簡單。推薦使用carchive,它的使用方法簡單且功能十分強大。首先還是用cfile宣告乙個物件,然後用這個物件的指標做引數宣告乙個carchive物件就可以非常方便地儲存各種複雜的資料型別了。它的使用方法見下例。

//對檔案進行寫操作

cstring strtemp;

cfile mfile;

mfile.open("d://dd//try.try",cfile::modecreate|cfile::modenotruncate|cfile::modewrite);

carchive ar(&mfile,carchive::store);

ar<<  ar.close();

mfile.close();

//對檔案進行讀操作

cfile mfile;

if(mfile.open("d://dd//try.try",cfile::moderead)==0)

return;

carchive ar(&mfile,carchive::load);

ar>>strtemp;

ar.close();

mfile.close();

carchive的 << 和》 操作符用於簡單資料型別的讀寫,對於cobject派生類的物件的訪問要使用readobject()和writeobject()。使用carchive的readclass()和writeclass()還可以進行類的讀寫,如:

//儲存caboutdlg類

ar.writeclass(runtime_class(caboutdlg));

//讀取caboutdlg類

cruntimeclass* mrunclass=ar.readclass();

//使用caboutdlg類

cobject* pobject=mrunclass->createobject();

((cdialog* )pobject)->domodal();

如果要進行的檔案操作只是簡單的讀寫整行的字串,建議使用cstdiofile,用它來進行此類操作非常方便,如下例。

cstdiofile mfile;

cfileexception mexcept;

mfile.open( "d://temp//aa.bat", cfile::modewrite, &mexcept);

cstring string="i am a string.";

mfile.writestring(string);

mfile.close();

4.臨時檔案的使用

正規軟體經常用到臨時檔案。臨時檔案的使用方法基本與常規檔案一樣,只是檔名應該呼叫函式gettempfilename()獲得。它的第乙個引數是建立此臨時檔案的路徑,第二個引數是建立臨時檔名的字首,第四個引數用於得到建立的臨時檔名。得到此臨時檔名以後,就可以用它來建立並操作檔案了,如:

char sztemppath[_max_path],sztempfile[_max_path];

gettemppath(_max_path, sztemppath);

gettempfilename(sztemppath,_t ("my_"),0,sztempfile);

cfile m_tempfile(sztempfile,cfile:: modecreate|cfile:: modewrite);

char m_char='a';

m_tempfile.write(&m_char,2);

m_tempfile.close();

5.檔案的複製、刪除等

mfc中沒有提供直接進行這些操作的功能,因而要使用sdk。sdk中的檔案相關函式常用的有copyfile()、createdirectory()、deletefile()、movefile()。它們的用法很簡單,可參考msdn。

判斷檔案是否存在

access(filename,mode);

[1]顯示對話方塊,取得檔名

cstring filepathname;

cfiledialog dlg(true);///true為open對話方塊,false為s***e as對話方塊

if (dlg.domodal() == idok)

filepathname=dlg.getpathname();

相關資訊:cfiledialog 用於取檔名的幾個成員函式:

假如選擇的檔案是c:/windows/test.exe

則(1)getpathname();取檔名全稱,包括完整路徑。取回c:/windows/test.exe

(2)getfiletitle();取檔案全名:test.exe

(3)getfilename();取回test

(4)getfileext();取副檔名exe

[2]開啟檔案

cfile file("c:/hello.txt",cfile::moderead);//唯讀方式開啟

//cfile::moderead可改為 cfile::modewrite(只寫),

//cfile::modereadwrite(讀寫),cfile::modecreate(新建)

例子:[3]移動檔案指標

file.seek(100,cfile::begin);///從檔案頭開始往下移動100位元組

file.seek(-50,cfile::end);///從檔案末尾往上移動50位元組

file.seek(-30,cfile::current);///從當前位置往上移動30位元組

file.seektobegin();///移到檔案頭

file.seektoend();///移到檔案尾

[4]讀寫檔案

讀檔案:

char buffer[1000];

file.read(buffer,1000);

寫檔案:

cstring string("自強不息");

file.write(string,8);

[5]關閉檔案

file.close();

MFC之基本的檔案操作

1.我們要做的乙個demo是建立文字檔案並進行讀寫資料的操作 cfile類是mfc中檔案操作的積累,他派生自cobject,直接提供二進位制檔案的輸入輸出服務。本例將介紹如何使用cfile類進行將編輯框中的文字儲存好txt檔案中,並可以進行檔案的讀取操作。寫入檔案的 如下 讀取檔案的 如下 注意 在...

Linux下檔案和資料夾的基本操作

1.刪除檔案 rm 檔名 普通使用者下操作,如果不行 sudo rm 檔名 root下操作 2.刪除資料夾 sudo rm rf 資料夾名 root許可權下操作 3.新建乙個資料夾 mkdir 資料夾名 mkdir 120 得到乙個為120的資料夾 4.新建乙個檔案 touch 檔名.txt 如 r...

MFC的檔案操作

mfcfile.cpp 定義控制台應用程式的入口點。include stdafx.h include mfcfile.h ifdef debug define new debug new endif 唯一的應用程式物件 using namespace std lpcstr tchar lpctstr...