系統(tǒng)調(diào)用方式文件編程要點(diǎn)_第1頁(yè)
系統(tǒng)調(diào)用方式文件編程要點(diǎn)_第2頁(yè)
系統(tǒng)調(diào)用方式文件編程要點(diǎn)_第3頁(yè)
系統(tǒng)調(diào)用方式文件編程要點(diǎn)_第4頁(yè)
系統(tǒng)調(diào)用方式文件編程要點(diǎn)_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Linux 文件編程函數(shù)一 簡(jiǎn)述幾個(gè)基本知識(shí)1 Linux 應(yīng)用程序編程所用到的 函數(shù) ,主要有兩種方式提供:系統(tǒng)調(diào)用方式 函數(shù)庫(kù)方式2 如何學(xué)習(xí)這些函數(shù)?三步學(xué)習(xí)法:第一步: 借助工具書,查找函數(shù)名; Unix 環(huán)境高級(jí)編程 第二步:在Linux系統(tǒng)中,利用man命令查看函數(shù)信息,并 填寫 函數(shù)學(xué)習(xí)手冊(cè) 。第三步: 實(shí)踐, 編寫代碼 。3 VI 概念 文件描述符性質(zhì): 一個(gè)數(shù)字特別含義: 其功能類似于身份證號(hào)碼, 通過(guò)身份證號(hào)碼, 可以 將對(duì)應(yīng)的公民; 在 Linux 系統(tǒng)中, 每一個(gè) 打開(kāi)的 文件, 都對(duì)應(yīng) 一個(gè)數(shù)字, 通過(guò)這個(gè)唯一的數(shù)字, 可以找到這個(gè)打開(kāi)的文件, 并 對(duì)其進(jìn)行操作,比如

2、讀、寫等。二 首先學(xué)習(xí) 系統(tǒng)調(diào)用方式提供的函數(shù) 4 學(xué)習(xí)以下 7 個(gè)函數(shù)打開(kāi)文件 創(chuàng)建文件 關(guān)閉文件 讀文件 寫文件 文件定位 復(fù)制文件描述符5 打開(kāi)文件 open范例 1:打開(kāi)已經(jīng)存的文件 open.c#include #include #include void main()int fd;/* 文件描述符 */fd = open(/home/test.c,O_RDWR);if(fd0)printf(Open file fali!n);elseprintf(Open file sucessfully!n);范例 2:利用 open 函數(shù)創(chuàng)建新文件 open_creat.c#include #

3、include #include void main()int fd;/* 文件描述符 */fd = open(/home/test1.c,O_RDWR | O_CREAT,0755);if(fd0) printf(Open file fali!n);elseprintf(Open file sucessfully!n);6 創(chuàng)建文件 creat 在一書中,找到函數(shù)名利用 man creat 查找函數(shù)的幫助文件 編寫程序代碼#include #include #include #include void main()int fd;fd = creat(/home/test2.c,0621);

4、if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n);7 在一書中,找到函數(shù)名man close 查找函數(shù)原型編寫程序代碼 close.c#include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n);int ret;re

5、t = close(fd);if(ret = 0)printf(File has been closed!n); elseprintf(Fail to close!n);8 首先查找一下讀文件的函數(shù) read()man 所查詢的是一個(gè)手冊(cè),首先從章節(jié)一里面找關(guān)鍵字,如 果沒(méi)有找到,再到后續(xù)章節(jié)中找。man 2 readman 章節(jié)一 命令章節(jié)二 系統(tǒng)調(diào)用章節(jié)三 庫(kù)函數(shù)范例:#include void main()int fd; fd = open(/home/test1.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);el

6、se printf(Create file successfully!n);char buf10;/* 定義一個(gè)數(shù)組,有 10 個(gè)空間;用來(lái)存放 讀取出的數(shù)據(jù) */ssize_t count;count = read(fd,buf,5);/*將讀取出的字符存放到 buf指向的空間里面。 */if(count=-1) printf(Read fail!);elseprintf(Read %d Bytes.n,count);buf5=0; /* 這樣,讀取后的內(nèi)容無(wú)亂碼字符出現(xiàn)。 */printf(buf is %s.n,buf); /* 打印字符串 */ int ret; ret = close

7、(fd);if(ret = 0)printf(File has been closed!n); elseprintf(Fail to close!n);9 寫文件找到寫文件對(duì)應(yīng)的函數(shù) write() man 2 writewrite.c#include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n);char *b

8、uf = 987654321;ssize_t count;count = write(fd,buf,7); if(count=-1)printf(Write fail!);elseprintf(Write %d Bytes.n,count); printf(The original buf is %s.n,buf);int ret;ret = close(fd);if(ret = 0)printf(File has been closed!n);elseprintf(Fail to close!n);10 定位文件lseek()在此之前,先實(shí)驗(yàn)一下,能否在文件寫入之后,直接讀取剛 才寫入的內(nèi)容

9、?實(shí)驗(yàn)驗(yàn)證,沒(méi)有讀到數(shù)據(jù)。這就是我們要給大家介紹的文件讀寫指針。當(dāng)打開(kāi)文件時(shí),指針位于文件頭部,然而,當(dāng)對(duì)文件進(jìn)行讀 或者寫操作后,文件指針將發(fā)生變化。介紹,如何設(shè)置指針的位置。范例:#include #include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n); char *buf = 987654321;s

10、size_t count;count = write(fd,buf,7);if(count=-1) printf(Write fail!);elseprintf(Write %d Bytes.n,count); printf(The original buf is %s.n,buf);off_t ref;ref = lseek(fd,0,SEEK_SET);if(ref=-1) printf(Fail to shift.n);elseprintf(The distance to file_head is %d Bytes.n,ref);char buf_r10;/* 定義一個(gè)數(shù)組,有 10 個(gè)

11、空間;用來(lái)存放讀取出的數(shù)據(jù) */ssize_t count_r;count_r = read(fd,buf_r,5);/*將讀取出的字符存放到buf 指向的空間里面。 */if(count_r=-1)printf(Read fail!);elseprintf(Read %d Bytes.n,count_r);buf_r5=0; /* 這樣,讀取后的內(nèi)容無(wú)亂碼出現(xiàn)。 */ printf(buf_r is %s.n,buf_r); /* 打印讀出的字符串 */int ret;ret = close(fd);if(ret = 0)printf(File has been closed!n); els

12、eprintf(Fail to close!n);11 復(fù)制文件描述符 dup()可以利用新復(fù)制的文件描述符 fd_new 來(lái)操作 oldfd 所 指定的文件。就好比一個(gè)人有兩個(gè)身份證號(hào)碼。/* 利用 dup() 函數(shù)復(fù)制后的文件描述符不同于原來(lái)的, 但共同指向同一個(gè)文件 */范例: dup.c#include #include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elsepri

13、ntf(Create file successfully!n);char *buf = 987654321; ssize_t count;count = write(fd,buf,7);if(count=-1)printf(Write fail!);elseprintf(Write %d Bytes.n,count);printf(The original buf is %s.n,buf);off_t ref;ref = lseek(fd,0,SEEK_SET); if(ref=-1) printf(Fail to shift.n);elseprintf(The distance to fil

14、e_head is %d Bytes.n,ref);int newfd;newfd = dup(fd);if(newfd=-1)printf(Fail to copy the old fd.n); elseprintf(The new fd is %d.n,newfd);char buf_r10;/* 定義一個(gè)數(shù)組,有 10 個(gè)空間;用來(lái)存 放讀取出的數(shù)據(jù) */ssize_t count_r;count_r = read(newfd,buf_r,5);/* 將讀取出的字符存放 到 buf 指向的空間里面。 */if(count_r=-1) printf(Read fail!);elseprin

15、tf(Read %d Bytes.n,count_r);buf_r5=0; /* 這樣,讀取后的內(nèi)容無(wú)亂碼出現(xiàn)。 */ printf(buf_r is %s.n,buf_r); /*打印讀出的字符串*/int ret;ret = close(fd);if(ret = 0)printf(File has been closed!n);%dprintf(The oldfd and the newfd are& %d.n,fd,newfd);elseprintf(Fail to close!n);13 綜合實(shí)例編寫文件復(fù)制程序cp 參數(shù) 1 參數(shù) 2源文件 、目標(biāo)文件 將源文件的所有內(nèi)容復(fù)制到目標(biāo)文

16、件中; 思路:打開(kāi)源文件,打開(kāi)目標(biāo)文件; 讀取源文件的數(shù)據(jù),將數(shù)據(jù)寫入目標(biāo)文件; open read write 查閱編寫的函數(shù)手冊(cè)。源文件是什么、目標(biāo)文件是什么?通過(guò) main() 函數(shù)的參數(shù) 傳進(jìn)去。編寫文件復(fù)制程序 copy.c#include #include #include #include #include void main(int argc,char *argv)/*argv0 存放命令的名 字, argv1 存放參數(shù) 1, argv2 存放參數(shù) 2*/* 打開(kāi)源文件 */int fd_s;fd_s = open(argv1,O_RDONLY);/* 打開(kāi)目標(biāo)文件 */int fd_d;fd_d = open(argv2,O_RDWR | O_CREAT,0666);/* 讀取源文件數(shù)據(jù) */* 考慮當(dāng)讀取的數(shù)據(jù)量很大 */char buf512;int count=0;/* 實(shí)際讀取的數(shù)據(jù)量 */ while(count = read(fd_s,buf,512)0) /* 將讀取出的源文件數(shù)據(jù)寫入目標(biāo)文件 */ write(fd_d,buf,count);/* 關(guān)閉 */ close(fd_s); close(fd_d);運(yùn)行時(shí),是如何執(zhí)行的?*/答:./copy /home/test2.c /home/test3.c*中間的數(shù)字是什么意思呢?

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論