File IO outline

Source: Internet
Author: User
Tags flock

    1. The difference between a file stream and a file descriptor

A) any process will open 3 Stream objectsby default at run time (stdin, stdout, stderr) and they all have corresponding file descriptors, each with a file descriptor of 0 ,1,2, the value of the file descriptor that is opened later generally selects the minimum value that is not used

b) LINUX opens files for each process, in the private struct task_struct of the process (i.e., process PCB, provided by the kernel), For any process, this structure will be assigned to manage open file information for the table entries, to indicate the current process open file structure, the popular point is that each process is in the kernel of the struct task_struct There is an information table entry in the struct, which then points to the list of files opened by the process, and then the file descriptor in the file list points to the file structure information that is open.

c) Conversion of file descriptors to file streams

Linux provides the user layer with the function fileno () to read its file descriptor from the file stream

extern int Fileno (FILE *__stream)

The function fdopen () will implement the connection of a stream to a file descriptor

extern FILE *fdopen (int __fd, __const char *__modes), where the flow object needs to be manipulated using fprintf () .

With the above convection, a more in-depth understanding of file descriptors, then learn the POSIX standard file IO Management will have more experience.

    1. fopen (), Fread (), fwrite (), fclose (), open (), read (), write (), close (), creat (), Lseek ()

File Open flags logo at a glance:

O_rdonly

O_wronly

O_RDWR,

O_creat

o_excl if used with o_creat , if the file already exists error

O_noctty If you open the end device, do not use it as a Process control terminal

O_trunc If the file exists and is readable and writable, the contents of the file are emptied

O_append starting from the end of the file when reading and writing files

O_nonblock Open in nonblocking mode and return immediately after execution

O_ndelay with o_nonblock

O_sync synchronously opens the file, writes the cache before closing the file

O_largefile support larger than 2G File Open Operation under the system

File open Permissions at a glance:

S_i(r/w/x) (usr/grp/oth),S_irwxu, S_irwxo, S_IRWXG

The above is the SYSTEM function permission set identification, user functions are different, such as "r+""w+" and so on

Take your own notes and add them in.

    1. File Control fcntl () function , used to modify a particular property of a file descriptor, copy the file descriptor, get the file descriptor, set the file descriptor, get the file status settings file status
    2. mapping files to memory mmap ()
    3. Lock / Unlocking Files

Both the function flock () and the function fcntl () can provide a lock operation on the file, but flock () can only lock the entire file. You cannot lock an area, and fcntl () can provide content locking at any location.

    1. The basic operation of the directory stream ----- The directory file stores all the files in that directory and the subdirectory file information

A) open / Close the catalog file

DIR *opendir (const char *dirname)

DIR *closedir (const char *dirname) returns a directory stream pointer

b) read / Write directory contents

c) Locate directory contents

d) Adding and removing directories

e) Get the current work path

7. The above file operation is only basic about the contents of the file operation, there is a piece of content, is about the normal file, connection files and directory file attributes of the read and modify operations, this piece of content we learn, not to teach.

File IO outline

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.