VC檔案操作總結

2021-05-22 21:21:08 字數 1769 閱讀 8942

1.讀寫檔案

cstring strpath=「c://test.txt」;//檔案路徑

cfile file;

if (file.open(strpath,cfile::modecreate|cfile::modenotruncate|cfile::modewrite))//寫檔案

cstring strpath=「c://test.txt」;

cfile file;

if (file.open(strpath,cfile::modecreate|cfile::modenotruncate|cfile::moderead))//讀檔案

cstring strpath=「c://test.txt」;

cfile file;

if (file.open(strpath,cfile::modecreate|cfile::modenotruncate|cfile::modereadwrite))//讀寫檔案

2.重新命名檔案

//stroldpath,strnewpath分別代表舊的檔案和新的檔案的絕對路徑

bool renamefile(cstring stroldpath,cstring strnewpath)

return true;

3.重新命名資料夾/檔案

bool   movefile(

lpctstr   lpexistingfilename,         // 舊資料夾名稱

lpctstr   lpnewfilename               // 新資料夾名稱

); 或者

bool movefileex (

lpctstr lpexistingfilename,              //舊資料夾名稱

lpctstr lpnewfilename,                   //新資料夾名稱

dword dwflags

);或者如下函式

bool renamefolder(lpctstr lpszfrompath,lpctstr lpsztopath)

5.建立目錄

bool createdirectory(

lpctstr lppathname,                                       //建立的目錄路徑

lpsecurity_attributes lpsecurityattributes  //建立的目錄安全屬性

);返回值:

成功返回非0,失敗返回0。

呼叫getlasterror獲得額外的資訊.

return code

description

error_already_exists

指定的目錄已經存在.

error_path_not_found

如果中間有目錄不存在將出錯。想直接建立所有路徑目錄用shcreatedirectoryex函式.

6.刪除目錄

方法1:遞迴呼叫

//刪除資料夾目錄(非空)

bool deletedirectory(char* sdirname)

else }

} tempfind.close();

if(!removedirectory(sdirname))

return false;

return true;

} 方法2:shell api

bool deldirectory(lpctstr lpszpath)

VC 檔案操作

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

VC 檔案操作

try else catch cmemoryexception e e geterrormessage szbuff,max path afxmessagebox szbuff catch cfileexception e e geterrormessage szbuff,max path afxm...

VC操作Excel檔案

excel 簡單操作其實就是讀和寫,包括新增,刪除,修改 其實和對資料庫進行操作是一樣的,微軟提供了方法 1.首先獲得計算機內已經安裝的驅動名稱 sqlgetinstalleddrivers szbuf,cbbufmax,cbbufout 引數1 是字串,它將獲得所有驅動名稱,中間以null分隔,第...