標籤: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