File descriptor-based file manipulation under Linux (non-buffered)

Source: Internet
Author: User

1 File descriptor

The kernel maintains a record table of open files (implemented as a struct array) for each process, and the file descriptor is a small positive integer (0-1023) (struct array subscript), which represents an entry for the record table, which can be implemented by file descriptors and a set of file manipulation functions based on file descriptors, to read and write files , create, delete, and so on.

Commonly used file descriptor-based functions are open (open), creat (Create), close (off), read (read), write (write), ftruncate (change file size), Lseek (locate), Fsync (synchronous), Fstat (get file status), Fchmod (permissions), Flock (locking), Fcntl (Control file properties), DUP (copy), dup2, select, and IOCTL. File manipulation based on file descriptors is not a function of ANSI C. 2 Open, create, and close files both open and creat can be opened and created with the prototype:

#include <sys/types.h>//header File#include <sys/stat.h>#include<fcntl.h>intOpenConst Char*pathname,intFlags);//how file names are openedintOpenConst Char*pathname,intFlags, mode_t mode);//file name Open with permissionintCreat (Const Char*pathname, mode_t mode);//File name Permissions//It's not used anymore.The creat function is equivalent to the. Open (pathname,o_creat| o_trunc| O_wronly,mode);

The open () function returns 1 when there is an error, and the relevant parameters are as follows:

Both flags and mode are a set of masked composite values that flags represent the way to open or create, while mode represents the access rights of the file.

The following options are available for flags:

Mask

Meaning

O_rdonly

Open in read-only mode

O_wronly

Open in a write-only manner

O_rdwr

Open in a read-write manner

O_creat

If the file does not exist, create the file

O_excl

Only with O_creat, forcing open to fail if the file already exists

O_trunc

If the file exists, the length of the file is as long as 0

O_append

An appended way to open a file, each time the write is called, the file pointer is automatically first moved to the end of the file, for multiple processes to write the same file.

O_nonblock

Open non-blocking mode, no matter if there is no data read or wait, will return to the process immediately.

O_nodelay

Open non-blocking mode

O_sync

Open files synchronously and return only if the data is actually written to the physical device device

The options available for mode are:

S_irwxu 00700 permission that represents the file owner with readable, writable, and executable permissions.
S_IRUSR or s_iread,00400 permission that represents the owner of the file with readable permissions.
S_IWUSR or S_IWRITE,00200 permission that represents the file owner with writable permissions.
A s_ixusr or S_IEXEC,00100 permission that represents the owner of the file that has executable permissions.

S_IRWXG 00070 permission, which represents the file user group with readable, writable, and executable permissions.
S_IRGRP 00040 permission, which represents the file user group with readable permissions.
S_IWGRP 00020 permission, which represents the file user group with writable permissions.
S_IXGRP 00010 permissions, which represent the permissions that the file user group has to perform.

S_irwxo 00007 permissions, which represent other users with readable, writable, and executable permissions.
S_iroth 00004 permissions, with readable permissions on behalf of other users
S_iwoth 00002 permissions, which represent other users with writable permissions.
S_ixoth 00001 permissions, which represent other users with enforceable permissions.

However, it is usually used in the form of direct assignment of values, such as:

int fd = open ("1. txt", o_wronly | O_creat,0755);  // represents a permission to 755 if (-1 = = fd)    {perror ("open failed!\n");    Exit (-1);
}

Note: The file descriptor-based open function in Linux, for a nonexistent file, cannot be opened by O_wronly, and the o_creat option must be added. Close is used for file closing:

int Close (int fd); // FD represents a file descriptor, which is the return value when the file was previously created by Open or creat. 

After the file is used, you should call close to close it, and once close is called, the lock that the process adds to the file is freed, and the open reference count of the file is reduced by 1, and only the file's open reference count becomes 0, the file is actually closed. Kris_ Sample Code _1

/************************************************************************* > File name:my_open.c > Author: Krischou > Mail:[email protected] > Created Time:mon-10:27:49 AM CST *************************** *********************************************/#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) {    /*Open File int fd;//Descriptor FD = open (argv[1],o_rdonly);        if (fd = =-1) {perror ("Open failed");    Exit (1);    } return 0; */        intFD; FD= Open (argv[1],o_wronly | O_creat,0666); if(FD = =-1) {perror ("Open Failed"); Exit (1); }    return 0;}

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.