C 常用的檔案操作路徑函式

2022-08-09 08:00:18 字數 4356 閱讀 3304

shlwapi.dll中的實用api函式發布

在windows   system目錄下有這個動態鏈結庫

bool pathfileexists(lpctstr lpszpath)

功能:檢查檔案/路徑是否存在

lptstr pathfindfilename(lpctstr ppath)

功能:獲得路徑中的檔名

例如: pathfindfilename( 「c:\\program files\\file.txt」 ) = 「file.txt」。

如果路徑中不包含檔名,則返回值是最下面的目錄名。

lptstr pathfindextension(lpctstr ppath)

功能:獲取路徑或檔名中的副檔名

例如:pathfindextension( 「file.txt」 ) = 「.txt」

lptstr pathfindnextcomponent(lpctstr pszpath)

例如:pathfindnextcomponent( 「c:\\program files\\directory」 ) = 「program files\\

directory」

pathfindnextcomponent( 「c:\\program files」 ) = 「program files」

bool pathfindonpath( in out lptstr   pszfile , in lpctstr * ppszotherdirs)

功能:在指定的目錄中尋找檔案。

引數: 

pszfile  要尋找的檔名,確保 pszfile 有足夠的大小來容納 max_path 個字元。如果能找到指定的檔案,該引數將返回檔案的全路徑。

ppszotherdirs   該引數是首先要尋找的目錄列表,它可以為null,如果為null,函式將在系統目錄,當前目錄和由path設定的目錄中查詢。

lptstr pathgetargs(lpctstr pszpath)

功能:從路徑中分析引數

例如:lptstr pszparam = pathgetargs( 「notepad.exe c:\\temp.txt」 )

pszparam 返回 「c:\\temp.txt」

int pathgetdrivenumber(lpctstr lpsz)

功能:從路徑中分析碟符

返回值:成功返回 0 –25 ,代表(a – z),失敗返回 –1

bool pathiscontenttype(lpctstr pszpath,lpctstr pszcontenttype)

功能:檢查檔案是否為指定的contenttype

例如:pathiscontenttype( 「hello.txt」 , 「text/plain」 ) 返回true

pathiscontenttype( 「hello.txt」 , 「image/gif」 ) 返回false

bool pathisdirectory(lpctstr pszpath)

功能:檢查路徑是否為有效目錄。注意路徑不能包含檔名

bool pathisfilespec(lpctstr lpszpath)

功能:檢查路徑中是否帶有 『:』 和 『\』 分隔符

bool pathishtmlfile(lpctstr pszfile)

功能:檢查是否為html檔案

bool pathisprefix(in lpctstr  pszprefix,in lpctstr  pszpath)

功能:檢查路徑是否包含指定字首,字首如:」碟符:\\」 、「..\\」 、」.\\」

pathisprefix ( 「c:\\」 , 「c:\\hello.txt」 ) 返回       true

bool pathisrelative(lpctstr lpszpath)

功能:檢查路徑是否是相對路徑

bool pathisroot(lpctstr ppath)

功能:檢查路徑是否是根目錄

bool pathissameroot(lpctstr pszpath1,lpctstr pszpath2);

功能:比較兩個路徑是否在同一碟符

bool pathisunc(lpctstr pszpath );

功能:判斷路徑是否是unc格式

bool pathisurl(in lpctstr pszpath );

功能:判斷路徑是否是url

bool pathmatchspec( lpctstr pszfileparam,lpctstr pszspec)

功能:用帶有萬用字元(*號和?號)的字串去比較另乙個字串

例如:pathmatchspec( 「 , 「*sina.com*」 ) 返回true

pathmatchspec( 「c:\\hello.txt」 , 「*.txt」 ) 返回true

void pathremoveargs(lptstr pszpath);

功能:刪除路徑中帶有的引數

lptstr pathremovebackslash(lptstr lpszpath);

功能:刪除路徑結尾多餘的』\』符號

void pathremoveblanks(lptstr lpszstring);

功能:刪除字串頭尾的空格

void pathremoveextension( lptstr pszpath);

功能:刪除路徑後面的副檔名

bool pathremovefilespec( lptstr pszpath);

功能:刪除路徑後面的檔名和』\』符號。該函式可以分析出乙個檔案的路徑。

例:szpath = 「c:\\windows\\system32\\nt.dll」 ;

呼叫pathremovefilespec( szpath ) 後,szpath = 「c:\\windows\\system32」 

bool pathrenameextension( in out lptstr pszpath,lpctstr pszext);

功能:替換路徑後面的副檔名,如果pszpath後面不帶副檔名,則新的副檔名會被新增上去。

pszpath  pszpath至少需要max_path個字元的空間

pszext    新的副檔名,必需包含 . 號

void pathstrippath(lptstr pszpath );

功能:從路徑中分析出檔名

例: pathstrippath( 「c:\\test\\hello.txt」 ) => hello.txt

bool pathstriptoroot( lptstr szroot );

功能:從路徑中分析出碟符

void pathunquotespaces( lptstr lpsz );

功能:從帶引號的路徑中取出路徑

例:szpath = 「\」c:\\program files\」」 

pathunquotespaces( szpath )  => szpath = 「c:\\program files」

附另一路徑相關函式,由imagehlp提供

bool searchtreeforfile(

in lpstr rootpath,       

in lpstr inputpathname,  

out lpstr outputpathbuffer  

);功能:從指定目錄樹中尋找檔案

extractfilepath()

功能:返回完整檔名中的路徑

函式原型如下:

ansistring __fastcall extractfilepath;

extractfilepath和相近函式:

extractfiledrive :返回完整檔名中的驅動器,如"c:" 

extractfilepath:返回完整檔名中的路徑,最後帶「/」,如"c:\test\" 

extractfiledir:返回完整檔名中的路徑,最後不帶「/」 ,如"c:\test" 

extractfilename:返回完整檔名中的檔名稱 (帶副檔名),如"mytest.doc" 

extractfileext 返回完整檔名中的副檔名(帶.),如".doc" 

extractfiledir //這個沒有最後的 \ 

extractfilepath //這個最後有 \

expandfilename()

#include   "shlwapi.h"   

#   pragma   comment(lib,"shlwapi.lib")

參考文件 :

C檔案操作常用函式

函式名 fseek 功 能 重定位流上的檔案指標 用 法 int fseek file stream,long offset,int fromwhere 程式例 include long filesize file stream int main void long filesize file st...

C 檔案操作常用函式

c 檔案操作常用函式 fopen fclose ftell fread fseek fopen 功能 開啟檔案 定義函式 file fopen const char path,const char mode 輸入引數 path字串包含欲開啟的檔案路徑及檔名 mode字串則代表著流模式。常見模式為r ...

shlwapi常用的操作路徑函式

shlwapi.dll中的實用api函式發布 在windows system目錄下有這個動態鏈結庫 include shlwapi.h pragma comment lib,shlwapi.lib bool pathfileexists lpctstr lpszpath 功能 檢查檔案 路徑是否存在...