Linux----file I/O

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.