linux中标准I/O 文件I/O 及库
????????????????????????????????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 標(biāo)準(zhǔn) I? / O
?
fopen() 函數(shù)打開文件的方式
?
r / rb 只讀 文件必須存在
r+ / r+b 讀寫 文件必須存在
w / wb 只寫 文件存在則長度清零 不存在則創(chuàng)建
w+ / w+b 讀寫 其他 同w
a / ab 同w 且寫入的數(shù)據(jù)會被追加到文件末尾
a+ / a+b 讀寫 數(shù)據(jù)在文件末尾追加 其他同a
?
diff -ruN +第一個(gè)文件 +第二個(gè)文件 測試文件復(fù)制成功與否???
?
?
fgetc / fput 可操作文本文件和二進(jìn)制文件 效率較低
fgets / fputs 只能操作文本文件(原因是當(dāng)讀取到0 默認(rèn)為終止符) 效率較高
?
fread / fwrite 推薦使用
?
fllush(FILE *stream) 強(qiáng)制刷新緩存區(qū)
ftell (FILE *stream) 返回當(dāng)前流
的位置
fseek(FILE *stream, long offset, int whence) 設(shè)定流的位置 成功則返回0
whence 參數(shù)的設(shè)定 三個(gè)宏 SEEK_SET / SEEK_CUR / SEEK_END 分別是文件的起始 當(dāng)前和結(jié)尾位置
offset參數(shù)是偏移量
?
#nclude <errno.h>
ferror(FILE *stream) 流是否出錯(cuò) 有錯(cuò)返回1 否則返回0
feof(FILE *stream) 返回1表示文件已經(jīng)到末尾 否則返回0
?
#include <string.h>
fprintf(FILE *stream, const char *fmt......)
ps :把內(nèi)容寫入文件
sprintf(char *s, const char *fmt.....)
ps: 把內(nèi)容寫入緩沖區(qū)
?
sleep()程序休眠 需要添加頭文件 #include <unistd.h>
?
#include <time.h>
time(time *t) 獲取當(dāng)前時(shí)間 秒為單位
localltime()
?
使用時(shí)
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) 獲取時(shí)間 返回一個(gè)字符串 內(nèi)容分別是 星期幾(英語單詞) 月份(英語單詞) 一月中的第幾天 時(shí)分秒 年份
time_t t;
printf("%s", ctime(&t));
#include <fcntl.h>
int open(const char *path, int oflag,...) 用來打開和創(chuàng)建一個(gè)文件 成功則返回文件描述符 失敗返回EOF 可以是兩個(gè)參數(shù) 也可以是三個(gè)參數(shù) 兩個(gè)參數(shù)是打開文件 三個(gè)參數(shù)是創(chuàng)建文件(多出的參數(shù)是用來設(shè)置文件權(quán)限的)
第二個(gè)參數(shù)可選項(xiàng):
O_RDONLY:只讀的方式打開文件
O_WRONLY:只寫的方式打開文件
O_RDWR:讀寫的方式打開文件
只能選一個(gè)
?
O_CREAT: 如果文件不存在就創(chuàng)建一個(gè) 且需要通過第三個(gè)參數(shù)設(shè)置文件權(quán)限
O_EXCL: 如果使用O_CREAT時(shí) 文件存在 則這個(gè)會返回錯(cuò)誤信息 作用是判斷文件是否存在
O_TRUNC:如果文件存在 則刪除原有數(shù)據(jù)
O_APPEND:使用這個(gè)參數(shù) 寫入的方式都會被追加到文件尾部
?
第三個(gè)參數(shù) 文件權(quán)限 以八進(jìn)制數(shù)表示
?
pS:
if((fd == open("1.txt", O_RDWR | O_CREAT | O_EXCL, 0666)) < 0)
{
if(erron == EEXIST) 通過對全局變量進(jìn)行判斷 可是錯(cuò)誤原因
perror("exist error");//文件存在才發(fā)生的錯(cuò)誤
else
perror("other error ");
}
?
頭文件 #include <unistd.h>
int close(int fd) 關(guān)閉文件 成功返回0 失敗返回EOF
?
頭文件#include <unsitd.h>
ssize_t read(int fd ,void *buf, size_t count) 成功則返回實(shí)際讀取到的字節(jié)數(shù) 失敗則返回EOF
讀到末尾返回0
?
#include <unistd.h>
ssize_t write(int fd, void *buf, size_t count) 成功返回實(shí)際寫入的字節(jié) 失敗返回EOF
count 不應(yīng)超過buf大小
?
#include <unistd.h>
off_t lseek(int fd , off_t offset , intt whence)
成功返回文件當(dāng)前讀寫位置 出錯(cuò)返回EOF
?
?????????????????????????????????????????????????????????? 文件I/O
?
打開目錄文件函數(shù)
#include <dirent.h>
DIR *opendir(const char *name)
?
讀取目錄文件函數(shù)
#include <dirent.h>
struct dirent *readdir(DIR *dirp)
?
成功則返回一個(gè)目錄流下的一個(gè)目錄項(xiàng) 到末尾或者出錯(cuò)會返回NULL
?
關(guān)閉目錄文件函數(shù)
#include <dirent.h>
int closedir(DIR *dirp)
修改文件權(quán)限
?
#include <sys/stat.h>
int chmod(const char *path, mode_t mode)
int fchmod(int fd, mode_t mode)
成功返回0 錯(cuò)誤返回EOF
只有root用戶才有權(quán)限修改文件權(quán)限
?
獲取文件屬性
#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 錯(cuò)誤返回EOF
?
stat()是獲取目標(biāo)文件屬性
lstat() 是獲取鏈接文件屬性
?
struct stat結(jié)構(gòu)體的一些屬性
?
mode_t st_mode 類型和訪問權(quán)限
uid_t st_uid 所有者的ID
uid_t st_gid 用戶組ID
off_t st_size 文件大小
time_t st_mtime 最后修改時(shí)間
?
?
????????????????????????????????????????????????????????????????????? 靜態(tài)庫
?
生成靜態(tài)庫
ar crs lib+filename.a filename.o
?
查看庫中函數(shù)情況
nm lib+filefilename.a
?
怎么給一個(gè)程序鏈接一個(gè)庫
設(shè)測試程序?yàn)?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
數(shù)字是版本
為共享庫文件創(chuàng)建鏈接文件
ln -s libcommon.so.1 libcommon.so
?
測試
gcc -o test test.c -L+庫的路徑 -l+庫文件名稱
默認(rèn)鏈接共享庫 如果想優(yōu)先鏈接靜態(tài)庫 在后面加 -static
?
讓系統(tǒng)找到共享庫
?
1. 在環(huán)境變量中添加庫的路徑
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:+庫的路徑
?
2.把庫都復(fù)制到/lib 或/user/lib)
3.添加到/etc/ld.so.conf.d/*.conf文件 執(zhí)行l(wèi)dconfig刷新
?
總結(jié)
以上是生活随笔為你收集整理的linux中标准I/O 文件I/O 及库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LOL挖掘机厉害吗,值得买吗,急急急,求
- 下一篇: 进程常用指令 (从创建到回收 包含守护)