c 遍歷目錄用到的幾個函式

2021-06-14 14:40:26 字數 2694 閱讀 7181

int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode );

功 能: 確定檔案或資料夾的訪問許可權。即,檢查某個檔案的訪問方式,比如說是唯讀方式、只寫方式等。如果指定的訪問方式有效,則函式返回0,否則函式返回-1。

#include

引數說明:

filenpath

檔案或資料夾的路徑,

當前目錄直接使用檔案或資料夾名

備註:當該引數為檔案的時候,access函式能使用mode引數所有的值,當該引數為資料夾的時候,access函式值能判斷資料夾是否存在。在win nt 中,所有的資料夾都有讀和寫許可權

mode

要判斷的模式

在標頭檔案unistd.h中的預定義如下:

#define r_ok 4 /* test for read permission. */

#define w_ok 2 /* test for write permission. */

#define x_ok 1 /* test for execute permission. */

#define f_ok 0 /* test for existence. */

具體含義如下:

r_ok 只判斷是否有讀許可權

w_ok 只判斷是否有寫許可權

x_ok 判斷是否有執行許可權

f_ok 只判斷是否存在

int _chdir(const char *path);

標頭檔案:direct.h

功 能: 改變當前工作目錄

參 數:path 必選。path 可能包含驅動器。如果未指定驅動器,則當前驅動器上的預設目錄或資料夾。

返回值:成功返回0 ,失敗返回-1

異 常 :異常型別 錯誤號 條件

argumentexception   52   path 為空。

filenotfoundexception   76   指定的驅動器無效,或驅動器不可用。

備 注: chdir 函式更改預設目錄,但是不更改預設驅動器。例如,如果預設驅動器是 c,下面的語句更改驅動器 d 上的預設目錄,但 c 仍為預設驅動器:

安全注意:  chdir 函式需要

非託管**許可權,這可能會對它在部分信任情況下的執行產生影響。有關更多資訊,請參見 securitypermission 類和**

訪問許可權。

函式原型:long _findfirst( char *filespec, struct _finddata_t *fileinfo );

函式功能:搜尋與指定的檔名稱匹配的第乙個例項,若成功則返回第乙個例項的控制代碼,否則返回-1l

標頭檔案:io.h

函式原型:

int _findnext(intptr_t handle,struct _finddata_t *fileinfo );

所屬庫:io.h

函式功能:搜尋與_

findfirst函式提供的檔名稱匹配的下乙個例項,若成功則返回0,否則返回-1

引數說明:

struct _finddata_t的定義見於io.h

struct _finddata_t ;

int _findclose(    intptr_t handle );

/*關閉查詢*/

parameters

handle

search handle returned by a previous call to_findfirst.

return value

if successful,_findclosereturns 0. otherwise, it returns –1 and setserrnotoenoent, indicating that no more matching files could be found.

_findclose

#include "io.h"

#include "direct.h"

#include using std::cin;

using std::cout;

using std::endl;

using std::cerr;

#include using std::string;

int main()

if (dir.at(dir.length() - 1) != '\\')

if (_chdir(dir.c_str()) != 0)

_finddata_t fileinfo;

memset(&fileinfo, 0x0, sizeof(fileinfo));

intptr_t ifind = _findfirst("*", &fileinfo);

if (ifind == -1)

string filepath(dir + fileinfo.name);

cout << "name: " << filepath << endl;

while (_findnext(ifind, &fileinfo) == 0)

_findclose(ifind);

return 0;

}

上面****:

做acm用到的幾個C 庫函式

在做acm時往往利用庫函式可以既方便又高效的解決一些問題,下面就記錄下最近用到的庫函式來幫助自己熟悉stl,不定期跟新。1.sort 函式 stl自帶的排序函式,使用標頭檔案的 include 在排序問題中使用此函式可以省去很多精力。int main 此例中sort函式有兩個引數,第乙個是指向序列頭...

Mysql模糊查詢用到的幾個函式

mysql模糊查詢用到的幾個函式 在使用中用到查某個欄位中包括個別字母,可以使用locate instr position find in set 這幾個函式。1 locate使用 select fromtablenamewhere locate findkeyword 欄位名 0,找到返回大於0,...

c語言常用到的幾個冷知識

c語言常用到的幾個冷知識 一,巨集定義中的特殊引數 和 va args 1,這個特殊的巨集定義引數也特別有用.作為乙個預處理運算子,它可以把語言符號字串化 stringizing 例如我們定義的變數等.簡單的說就是把定義值變成字串,常用於列印 define stringint x x int tes...