Linux----檔案I/O

來源:互聯網
上載者:User

標籤:linux   檔案io   

1.檔案描述符:每次我們開啟一個檔案,就會得到一個對應於該檔案的較小的整數,這個整數就是這個檔案的檔案描述符。在shell操作中,0,1,2這三個檔案描述附總是開啟的,通常是指向shell運行所在的終端。0對應於標準輸入,1對應於標準輸出,2對應於標準錯誤。因為0,1,2這三個檔案描述符總是開啟的,所以一般我們開啟一個檔案時,該檔案所對應的檔案描述符為3,再開啟一個檔案時,新開啟的檔案描述符為4,以此類推...

舉例:

#include <iostream>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>using namespace std;int main(){int fd1, fd2, fd3;fd1 = open("1.txt", O_RDONLY);     //open the first filefd2 = open("2.txt", O_RDONLY);    //open the second filefd3 = open("3.txt", O_RDONLY);    //open the third filecout << fd1 << endl;                     //the first file describecout << fd2 << endl;                     //the second file describecout << fd3 << endl;                     //the third file describereturn 0;}
運行結果:

[email protected]:~$ ./fd345
通過上述結果,可以得知,新開啟的檔案的檔案描述符從3開始,依次為3,4,5,6...等等。


2.與檔案I/O相關最常用的系統調用:

   open()

   write()

   read()

   close()

   具體用法使用 man 2 open, man 2 write等具體查看。


3.

Linux----檔案I/O

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.