Linux/C + +: File operations--open, read, write

Source: Internet
Author: User

Linux/C + +: File operations--open, create, close

File descriptor

Kernel (kernel) to access the file using the file descriptor (descriptor). The file descriptor is a non-negative integer. When you open an existing file or create a new file, the kernel returns a file descriptor. Read-write files also need to use file descriptors to specify which files to read and write. The system kernel maintains a list of file descriptors for each process. as follows:


The kernel Assignment file descriptor rule is that it is always allocated unused and is minimal. The first three file descriptors: 0, 1, 2, are already occupied by default. Of course, you can close it first. such as closing 1, redistribution, can lead to redirection.


Function prototypes

#include <fcntl.h>

int open (const char *path, int flag, .../*mode_t mode*/);

Function: Open File

Return value: Successful, returns the file descriptor; 1 If an error occurs

Parameters:

Path file paths

The open mode of the flag file

Common mode: o_rdonly read-only O_wronly Write only O_rdwr read/write o_exec perform O_search search (applies to directory) (must, five select one)

Optional: o_append append o_creat file does not exist, then create.

... File permissions


Function prototypes

#include <fcntl.h>

int Create (const char *path, mode_t mode);

Features: Creating files

Return value: Successful, returns the file descriptor; 1 If an error occurs

Parameters:

Path file paths

Mode file permissions


Function prototypes

#include <unistd.h>

int close (int fd);

Function: Close open files, release resources

Parameter: FD is pending file descriptor

Return: Success, return 0; error, return-1

It is necessary to note that when the process is finished, the kernel shuts down the files opened by the process.



Directory of all content

    • CCPP Blog Directory


Linux/C + +: File operations--open, read, write

Related Article

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.