Linux 常用函式

2021-10-06 01:18:02 字數 1661 閱讀 3109

檔案i/o

檔案描述符,有點對應windows的控制代碼, 檔案io操作的標頭檔案在unistd.h 和fcntl.h 

重點函式:open、read、write、lseek、close、fcntl

如建立乙個新檔案:open(path, o_rewr|o_create| o_trunc,mode);

檔案與目錄

1.stat或fstat函式。  返回stat結構,最重要的變數是 mode_t    st_mode; //包括檔案型別和許可權  對應於ls -l

s_isreg()普通檔案    s_isdir()目錄檔案

s_irusr  使用者讀  s_iwusr  使用者寫    s_ixusr 使用者執行   對應於chmod 

2. chmod 函式,改變檔案訪問許可權 。s_irwxu  s_irusr s_irwxg  s_irwxo   

3.chown 更改檔案的使用者id和組id.

4.truncate檔案截斷,檔案長度增加時可能有空洞

5.rename檔案或目錄重新命名

6.futimens、utimes檔案的訪問和修改時間

7.mkdir建立目錄

8.rmdir刪除目錄

9.chdir更改當前工作目錄

10.getcwd獲取當前工作目錄完整的絕對路徑名

io流, 

1.fopen開啟乙個標準的io流。

2.fclose關閉乙個開啟的流。

3.每次乙個字元的io  getc、getchar. putc

4.每次一行io. gets、fgets、puts、fputs

5.直接io.fread、fwrite.

格式化io

1.格式化輸出

printf、fprintf、dprintf、sprintf、snprintf

2.格式化輸入

scan、fscan、sscan

口令檔案

1.getpwuid 獲取口令檔案,結構為psswd

2.getspent獲取陰影口令,結構為spwd

3.getgrgid 、getgrent 獲取組檔案資訊

4.uname、獲取與主機和作業系統資訊

5.gethostname 獲取主機名字

6.time、gettimeofday獲取當前的時間和日期 .是以2023年經過的秒數為標準的。返回time_t結構. 精度只能到秒,如果想更高的精度,需要使用timespectimeval來處理。

一般由函式int clock_gettime(clockid_t  clock_id, struct timespec *tp)獲取特定時鐘的時間,

clock_id常用如下4種時鐘:

clock_realtime  系統當前時間,從2023年1.1日算起

clock_monotonic 系統的啟動時間,不能被設定

clock_process_cputime_id 本程序執行時間

clock_thread_cputime_id 本執行緒執行時間

Linux 常用函式 memcpy函式

linux 常用函式 memcpy函式 原型 extern void memcpy void dest,void src,unsigned int count 用法 include 功能 由src所指記憶體區域複製count個位元組到dest所指記憶體區域。說明 src和dest所指記憶體區域不能重...

linux常用庫函式

1.setenv getenv 可以用來做全域性傳參 表頭檔案 include 定義函式 int setenv const char name,const char value,int overwrite 標頭檔案 stdlib.h 用 法 char getenv char envvar 標頭檔案 ...

linux常用函式記錄

本文 一 獲取 設定linux系統的網路資訊 1.獲取ip位址 子網掩碼位址 mac位址 廣播位址 include include include include include include int getnetworkinfo void char ip 32 char broadaddr 32...