![系統(tǒng)調(diào)用方式文件編程_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/29/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa1.gif)
![系統(tǒng)調(diào)用方式文件編程_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/29/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa2.gif)
![系統(tǒng)調(diào)用方式文件編程_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/29/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa3.gif)
![系統(tǒng)調(diào)用方式文件編程_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/29/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa4.gif)
![系統(tǒng)調(diào)用方式文件編程_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/29/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa/8004f7d5-80ac-462c-93ef-0b2ba0ec8efa5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Linux文件編程函數(shù)一 簡述幾個(gè)基本知識1 Linux應(yīng)用程序編程所用到的函數(shù),主要有兩種方式提供: 系統(tǒng)調(diào)用方式 函數(shù)庫方式2 如何學(xué)習(xí)這些函數(shù)?三步學(xué)習(xí)法:第一步:借助工具書,查找函數(shù)名;Unix環(huán)境高級編程第二步:在Linux系統(tǒng)中,利用man命令查看函數(shù)信息,并填寫函數(shù)學(xué)習(xí)手冊。第三步:實(shí)踐,編寫代碼。3 VI概念文件描述符 性質(zhì): 一個(gè)數(shù)字 特別含義:其功能類似于身份證號碼,通過身份證號碼,可以將對應(yīng)的公民;在Linux系統(tǒng)中,每一個(gè) 打開的 文件,都對應(yīng)一個(gè)數(shù)字,通過這個(gè)唯一的數(shù)字,可以找到這個(gè)打開的文件,并對其進(jìn)行操作,比如讀、寫等。二 首先學(xué)習(xí)系統(tǒng)調(diào)用方式提供的函數(shù)4 學(xué)習(xí)以
2、下7個(gè)函數(shù)打開文件 創(chuàng)建文件 關(guān)閉文件 讀文件 寫文件 文件定位 復(fù)制文件描述符5 打開文件open范例1:打開已經(jīng)存的文件 open.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>void main()int fd;/*文件描述符*/fd = open("/home/test.c",O_RDWR);if(fd<0) printf("Open file fali!n");else printf("Open file suces
3、sfully!n");范例2:利用open函數(shù)創(chuàng)建新文件 open_creat.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>void main()int fd;/*文件描述符*/fd = open("/home/test1.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("Open file fali!n");else printf("Open file sucessfully!
4、n");6 創(chuàng)建文件creat 在一書中,找到函數(shù)名利用man creat查找函數(shù)的幫助文件編寫程序代碼#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main()int fd;fd = creat("/home/test2.c",0621);if(fd<0) printf("create file fail!n");else printf("Create fi
5、le successfully!n");7 在一書中,找到函數(shù)名 man close 查找函數(shù)原型 編寫程序代碼close.c#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");
6、else printf("Create file successfully!n"); int ret;ret = close(fd);if(ret = 0) printf("File has been closed!n");else printf("Fail to close!n");8 首先查找一下讀文件的函數(shù)read()man所查詢的是一個(gè)手冊,首先從章節(jié)一里面找關(guān)鍵字,如果沒有找到,再到后續(xù)章節(jié)中找。man 2 readman 章節(jié)一 命令 章節(jié)二 系統(tǒng)調(diào)用 章節(jié)三 庫函數(shù)范例:#include <fcntl.h>v
7、oid main()int fd;fd = open("/home/test1.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n");char buf10;/*定義一個(gè)數(shù)組,有10個(gè)空間;用來存放讀取出的數(shù)據(jù)*/ ssize_t count; count = read(fd,buf,5);/*將讀取出的字符存放到buf指向的空間里面。*/if(count=-1) print
8、f("Read fail!");else printf("Read %d Bytes.n",count);buf5='0' /*這樣,讀取后的內(nèi)容無亂碼字符出現(xiàn)。*/printf("buf is %s.n",buf); /*打印字符串*/ int ret;ret = close(fd);if(ret = 0) printf("File has been closed!n");else printf("Fail to close!n");9 寫文件 找到寫文件對應(yīng)的函數(shù)write(
9、) man 2 writewrite.c#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n&quo
10、t;); char *buf = "987654321"ssize_t count;count = write(fd,buf,7);if(count=-1) printf("Write fail!");else printf("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");e
11、lse printf("Fail to close!n");10 定位文件lseek()在此之前,先實(shí)驗(yàn)一下,能否在文件寫入之后,直接讀取剛才寫入的內(nèi)容?實(shí)驗(yàn)驗(yàn)證,沒有讀到數(shù)據(jù)。這就是我們要給大家介紹的文件讀寫指針。當(dāng)打開文件時(shí),指針位于文件頭部,然而,當(dāng)對文件進(jìn)行讀或者寫操作后,文件指針將發(fā)生變化。介紹,如何設(shè)置指針的位置。范例:#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.
12、h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n"); char *buf = "987654321"ssize_t count;count = write(fd,buf,7);if(count=-1) printf("Write fail!"
13、);else printf("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");else printf("The distance to file_head is %d Bytes.n",ref); char buf_r10;/*定義一個(gè)數(shù)組,有10個(gè)空間;用來存放讀取出的數(shù)據(jù)*/ s
14、size_t count_r; count_r = read(fd,buf_r,5);/*將讀取出的字符存放到buf指向的空間里面。*/if(count_r=-1) printf("Read fail!");else printf("Read %d Bytes.n",count_r);buf_r5='0' /*這樣,讀取后的內(nèi)容無亂碼出現(xiàn)。*/printf("buf_r is %s.n",buf_r); /*打印讀出的字符串*/ int ret;ret = close(fd);if(ret = 0) printf(&q
15、uot;File has been closed!n");else printf("Fail to close!n");11 復(fù)制文件描述符 dup()可以利用新復(fù)制的文件描述符fd_new來操作oldfd所指定的文件。就好比一個(gè)人有兩個(gè)身份證號碼。/*利用dup()函數(shù)復(fù)制后的文件描述符不同于原來的,但共同指向同一個(gè)文件*/范例:dup.c#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include
16、<fcntl.h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n"); char *buf = "987654321"ssize_t count;count = write(fd,buf,7);if(count=-1) printf("Write f
17、ail!");else printf("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");else printf("The distance to file_head is %d Bytes.n",ref); int newfd;newfd = dup(fd);if(newf
18、d=-1) printf("Fail to copy the old fd.n");else printf("The new fd is %d.n",newfd);char buf_r10;/*定義一個(gè)數(shù)組,有10個(gè)空間;用來存放讀取出的數(shù)據(jù)*/ ssize_t count_r; count_r = read(newfd,buf_r,5);/*將讀取出的字符存放到buf指向的空間里面。*/if(count_r=-1) printf("Read fail!");else printf("Read %d Bytes.n"
19、;,count_r);buf_r5='0' /*這樣,讀取后的內(nèi)容無亂碼出現(xiàn)。*/printf("buf_r is %s.n",buf_r); /*打印讀出的字符串*/ int ret;ret = close(fd);if(ret = 0)printf("File has been closed!n");printf("The oldfd and the newfd are %d & %d.n",fd,newfd); else printf("Fail to close!n");13 綜合實(shí)
20、例編寫文件復(fù)制程序cp 參數(shù)1 參數(shù)2源文件 、目標(biāo)文件將源文件的所有內(nèi)容復(fù)制到目標(biāo)文件中;思路:打開源文件,打開目標(biāo)文件;讀取源文件的數(shù)據(jù),將數(shù)據(jù)寫入目標(biāo)文件;open read write查閱編寫的函數(shù)手冊。源文件是什么、目標(biāo)文件是什么?通過main()函數(shù)的參數(shù)傳進(jìn)去。編寫文件復(fù)制程序copy.c#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main(int argc,char *argv)/*argv0存放命令的名字,argv1存放參數(shù)1,argv2存放參數(shù)2*/
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國硅膠直膠管市場調(diào)查研究報(bào)告
- 2025至2031年中國歐式風(fēng)撐行業(yè)投資前景及策略咨詢研究報(bào)告
- 2025至2030年中國橡膠海綿球數(shù)據(jù)監(jiān)測研究報(bào)告
- 2025至2030年中國固體阻燃潤滑劑數(shù)據(jù)監(jiān)測研究報(bào)告
- 2025至2030年中國反光絲數(shù)據(jù)監(jiān)測研究報(bào)告
- 2025年中國軸向電接點(diǎn)壓力表市場調(diào)查研究報(bào)告
- 2025年中國蓮子酥市場調(diào)查研究報(bào)告
- 建筑工程用機(jī)械設(shè)備的維護(hù)保養(yǎng)策略考核試卷
- 2025-2030年古典園林石雕藝術(shù)行業(yè)跨境出海戰(zhàn)略研究報(bào)告
- 2025-2030年復(fù)古陶瓷茶具托盤企業(yè)制定與實(shí)施新質(zhì)生產(chǎn)力戰(zhàn)略研究報(bào)告
- 中國氫內(nèi)燃機(jī)行業(yè)發(fā)展環(huán)境、市場運(yùn)行格局及前景研究報(bào)告-智研咨詢(2024版)
- 《自然保護(hù)區(qū)劃分》課件
- 2024年湖南高速鐵路職業(yè)技術(shù)學(xué)院高職單招數(shù)學(xué)歷年參考題庫含答案解析
- 上海鐵路局招聘筆試沖刺題2025
- 學(xué)校食堂餐廳管理者食堂安全考試題附答案
- 同等學(xué)力英語申碩考試詞匯(第六版大綱)電子版
- 中日合同范本
- T-CARM 002-2023 康復(fù)醫(yī)院建設(shè)標(biāo)準(zhǔn)
- 《康復(fù)按摩知識》課件
- 日立mcub02板使用說明書
- 干貨:影視制作報(bào)價(jià)表-簡潔版(范例)
評論
0/150
提交評論