????????????????????????????????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 標準 I? / O
?
fopen() 函數打開文件的方式
?
r / rb 只讀 文件必須存在
r+ / r+b 讀寫 文件必須存在
w / wb 只寫 文件存在則長度清零 不存在則創建
w+ / w+b 讀寫 其他 同w
a / ab 同w 且寫入的數據會被追加到文件末尾
a+ / a+b 讀寫 數據在文件末尾追加 其他同a
?
diff -ruN +第一個文件 +第二個文件 測試文件復制成功與否???
?
?
fgetc / fput 可操作文本文件和二進制文件 效率較低
fgets / fputs 只能操作文本文件(原因是當讀取到0 默認為終止符) 效率較高
?
fread / fwrite 推薦使用
?
fllush(FILE *stream) 強制刷新緩存區
ftell (FILE *stream) 返回當前流
的位置
fseek(FILE *stream, long offset, int whence) 設定流的位置 成功則返回0
whence 參數的設定 三個宏 SEEK_SET / SEEK_CUR / SEEK_END 分別是文件的起始 當前和結尾位置
offset參數是偏移量
?
#nclude <errno.h>
ferror(FILE *stream) 流是否出錯 有錯返回1 否則返回0
feof(FILE *stream) 返回1表示文件已經到末尾 否則返回0
?
#include <string.h>
fprintf(FILE *stream, const char *fmt......)
ps :把內容寫入文件
sprintf(char *s, const char *fmt.....)
ps: 把內容寫入緩沖區
?
sleep()程序休眠 需要添加頭文件 #include <unistd.h>
?
#include <time.h>
time(time *t) 獲取當前時間 秒為單位
localltime()
?
使用時
time_t t;
struct tm *tp;
?
time(&t);
tp = localtime(&t);
tp->tm_year,tp->tm_mon, tp->tm_mday, tp->tm-hour , tp->tm_min, tp->tm_sec
?
char ctime(const time_t *timer) 獲取時間 返回一個字符串 內容分別是 星期幾(英語單詞) 月份(英語單詞) 一月中的第幾天 時分秒 年份
time_t t;
printf("%s", ctime(&t));
#include <fcntl.h>
int open(const char *path, int oflag,...) 用來打開和創建一個文件 成功則返回文件描述符 失敗返回EOF 可以是兩個參數 也可以是三個參數 兩個參數是打開文件 三個參數是創建文件(多出的參數是用來設置文件權限的)
第二個參數可選項:
O_RDONLY:只讀的方式打開文件
O_WRONLY:只寫的方式打開文件
O_RDWR:讀寫的方式打開文件
只能選一個
?
O_CREAT: 如果文件不存在就創建一個 且需要通過第三個參數設置文件權限
O_EXCL: 如果使用O_CREAT時 文件存在 則這個會返回錯誤信息 作用是判斷文件是否存在
O_TRUNC:如果文件存在 則刪除原有數據
O_APPEND:使用這個參數 寫入的方式都會被追加到文件尾部
?
第三個參數 文件權限 以八進制數表示
?
pS:
if((fd == open("1.txt", O_RDWR | O_CREAT | O_EXCL, 0666)) < 0)
{
if(erron == EEXIST) 通過對全局變量進行判斷 可是錯誤原因
perror("exist error");//文件存在才發生的錯誤
else
perror("other error ");
}
?
頭文件 #include <unistd.h>
int close(int fd) 關閉文件 成功返回0 失敗返回EOF
?
頭文件#include <unsitd.h>
ssize_t read(int fd ,void *buf, size_t count) 成功則返回實際讀取到的字節數 失敗則返回EOF
讀到末尾返回0
?
#include <unistd.h>
ssize_t write(int fd, void *buf, size_t count) 成功返回實際寫入的字節 失敗返回EOF
count 不應超過buf大小
?
#include <unistd.h>
off_t lseek(int fd , off_t offset , intt whence)
成功返回文件當前讀寫位置 出錯返回EOF
?
?????????????????????????????????????????????????????????? 文件I/O
?
打開目錄文件函數
#include <dirent.h>
DIR *opendir(const char *name)
?
讀取目錄文件函數
#include <dirent.h>
struct dirent *readdir(DIR *dirp)
?
成功則返回一個目錄流下的一個目錄項 到末尾或者出錯會返回NULL
?
關閉目錄文件函數
#include <dirent.h>
int closedir(DIR *dirp)
修改文件權限
?
#include <sys/stat.h>
int chmod(const char *path, mode_t mode)
int fchmod(int fd, mode_t mode)
成功返回0 錯誤返回EOF
只有root用戶才有權限修改文件權限
?
獲取文件屬性
#include <sys/stat.h>
int stat(const char *path, struct stat *buf)
int lstat(const char *path, struct stat *buf)
int fstat(int fd, struct stat *buf)
成功返回0 錯誤返回EOF
?
stat()是獲取目標文件屬性
lstat() 是獲取鏈接文件屬性
?
struct stat結構體的一些屬性
?
mode_t st_mode 類型和訪問權限
uid_t st_uid 所有者的ID
uid_t st_gid 用戶組ID
off_t st_size 文件大小
time_t st_mtime 最后修改時間
?
?
????????????????????????????????????????????????????????????????????? 靜態庫
?
生成靜態庫
ar crs lib+filename.a filename.o
?
查看庫中函數情況
nm lib+filefilename.a
?
怎么給一個程序鏈接一個庫
設測試程序為 test.c
gcc -o test test.c -L+庫的路徑 -l+庫文件名稱
?
????????????????????????????????????????????????????????????? 共享庫
?
生成共享庫
gcc -c fPIC filename01.c filename02.c -Wall
gcc -shared -o libcommon.so.1 filename01.o filename.o
數字是版本
為共享庫文件創建鏈接文件
ln -s libcommon.so.1 libcommon.so
?
測試
gcc -o test test.c -L+庫的路徑 -l+庫文件名稱
默認鏈接共享庫 如果想優先鏈接靜態庫 在后面加 -static
?
讓系統找到共享庫
?
1. 在環境變量中添加庫的路徑
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:+庫的路徑
?
2.把庫都復制到/lib 或/user/lib)
3.添加到/etc/ld.so.conf.d/*.conf文件 執行ldconfig刷新
?