1. File Descriptor: Each time we open a file, we get a smaller integer corresponding to the file, which is the file descriptor of the file. In a shell operation, the 0,1,2 three file descriptions are always open, usually pointing to the terminal where the shell is running. 0 corresponds to standard input, 1 corresponds to standard output, and 2 corresponds to standard error. Because 0,1,2 these three file descriptor is always open, so generally when we open a file, the file corresponding to the file descriptor 3, and then open a file, the newly opened file descriptor is 4, and so on ...
Example:
#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;}
Operation Result:
[Email protected]:~$./fd345
with these results, you can learn that the file descriptor for the newly opened file starts with 3, 3,4,5,6 ... and so on .。
2. Most commonly used system calls related to file I/O:
Open ()
Write ()
Read ()
Close ()
The specific usage uses Man 2 open, the Man 2 write and so on specific view.
3.
Linux----file I/O