C C 讀取檔案的多種方式

2021-08-08 19:45:02 字數 2097 閱讀 6079

int fopentest()

while(1)

printf("%c", c);

}fclose(file);

return 0;

}

#include #include int main() 

char buf[1024];

while(1)

printf("%s\n", buf);

}fclose(fp);

return 0;

}

windows:

#include string folder = "d:/pictures";

if (_access(folder.c_str(), 0) == -1)

cout << "目錄:" << folder << endl;

linux:

#include int access(const char* _filename, int _accessmode)
該函式功能為確定檔案或資料夾的訪問許可權,如果指定的訪問許可權有效,則函式返回0,否則返回-1

filename可以是檔案路徑,也可以是資料夾路徑,可以使用絕對路徑或相對路徑

_accessmode表示要驗證的檔案訪問許可權,有可讀、可寫、可執行以及是否存在四種許可權,當filename表示資料夾時僅能查詢檔案夾是否存在

_accessmode:

標頭檔案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 只判斷是否存在

在巨集定義裡面分別對應:

00 只存在

02 寫許可權

04 讀許可權

06 讀和寫許可權

參考文章:

// string::find_last_of

#include // std::cout

#include // std::string

#include // std::size_t

void splitfilename (const std::string& str)

int main ()

splitting: /usr/bin/man

path: /usr/bin

file: man

splitting: c:\windows\winhelp.exe

path: c:\windows

file: winhelp.exe

第五: 寫入檔案

bool writelog(const char *log) ;

// 引數驗證

if (!strcmp(log, "") || !log)

// 建立檔案目錄

if (_access("data", 0) != 0)

}printf("目錄建立成功\n");

} // 組合檔案目錄

snprintf(filepath, 128, "%s\\%s", "data", "mylog.log");

// 寫入檔案

pf = fopen(filepath, "a+");

if (!pf) return false;

fwrite(log, strlen(log), sizeof(char), pf);

fclose(pf);

return true;

}

c 多種方式讀取檔案(觀察注意處的不同)

要讀取的檔案 逐個字元讀取 方式一 讀取 include includeusing namespace std int main infile noskipws 強制讀入每乙個字元,包括空格 轉折 while infile c i cout endl c i 0 注意,保證strlen c 的值正確...

讀取檔案的方式

讀取檔案的方式 1.for的方式 資料量下的時候可以使用,或者每行資料都是長度差不多的時候,可以使用,如果有部分行數資料超多或者不規範的話,建議使用while方式,while方式可以固定每次讀取的資源數,便於記憶體的運轉 with open test.txt mode rt encoding utf...

python讀取測試資料的多種方式

目錄 1 建立乙個config.ini或者.conf檔案,這種方法就是ini檔案的讀取,如下 api url www.taobao.com method get mysql db hello port 3306 2 使用python的configparser庫讀取,如下 from configpar...