Home > OS >  Study the Linux file I/O
Study the Linux file I/O

Time:11-16

Linux system programming: simple file IO operations
A file descriptor:
(1) the file descriptor is essentially a number, this number said in a process of a particular meaning, when we open to open a file, the operating system in memory to build some of the data structure to represent the dynamic file, and then return to the application a number as a file descriptor, it and maintain the whole dynamic documents in our memory the data structure of the hook on the binding, after our application if you need the whole dynamic file operation, only need to use the file descriptor to distinguish,
(2) according to the practice, the UNIX shell file descriptors 0 standard input associated with process, linked to the standard output file descriptor 1, 2 associated with standard error file descriptors, in accord with POSIX. 1 application, magic number 0, although has been standardized, but should replace them as symbolic constant STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO in order to improve the readability, these constant defined in & lt; Unistd. H> File,
(3) the file descriptor is used to distinguish between a program to open multiple files,
(4) the file descriptor is the scope of the current process, out of the current process the whole file descriptor is meaningless,
(5) Open the returned fd must record is good, to the entire file after all the operation depends on the fd to correspond to the file, finally close the file also need fd to close the specified file, if we close the file before lost my fd, then the file can not be closed can't go to read and write,
Second, what is the I/O
1, the input/output is the process of copying data between main memory and the external devices
(1) equipment - & gt; Memory (input)
(2) memory - & gt; Equipment (output)
2, for Linux, there are two types of I/O:
ANSIC (1) to provide the standard I/O library called high I/O, also known as I/O with buffer
(2) the low-level I/O, usually also known as I/O without buffer

Three, file IO related header file:
General header files in the/usr/include the following, this is the standard C program header files, if you are in front of the header file added & lt; Sys/* & gt; That means this is a system call function header files, in the/usr/include/sys,
#include
Symbolic constant is the POSIX standard Unix system definition of the definition of symbolic constant header files, contains many Unix system service function prototype, read write functions and getpid function, for example,
#include
An FCNTL. J h, is the standard Unix generic header file, which contains the correlation function is open, an FCNTL, shutdown, unlink, fclose
#include
Data types, basic system are the basic data types of Unix/LINUX system header files, including size_t, time_t, pid_t types, such as
#include
File status, is the Unix/linx system defined file status of pseudo scalar firing file contains the following types:
Dev_t the st_dev Device ID of the Device containing the file.
Ino_t st_ino File serial number.
Mode_t st_mode Mode of the file (see below).
Nlink_t st_nlink Number of hard links to the file.
Uid_t st_uid User ID of the file.
Gid_t st_gid Group ID of the file.
Dev_t st_rdev Device ID (if the file is a character or block special).
Off_t st_size For regular files, the file size in bytes.
For -- symbolic links, the length in bytes of the
The pathname contained in the -- link.

For a Shared memory object, the length in bytes.

For a typed the memory object, the length in bytes.

For other file types, the use of this field is
Unspecified.
Time_t st_atime Time of the last access.
Time_t st_mtime Time of the last data modification.
Time_t st_ctime Time of the last status change.
Int chmod (const char *, mode_t);
Int fchmod (int, mode_t);
Int the fstat (int, struct stat *);
Int lstat (const char * restrict, struct stat * restrict);
Int the mkdir (const char *, mode_t);
Int mkfifo (const char *, mode_t);
Int mknod (const char *, mode_t dev_t);
Int the stat (const char * restrict, struct stat * restrict);
Mode_t umask (mode_t);
Use stat function most likely is the ls - l command, with it you can get all the information about a file.
#include
In system programming errors usually by function return value ray said, and through the special variable errno to describe,
In the global variable errno & lt; Errno. H> Declared in a header file is as follows:
Exitern int errno
Error handling function
Perror
The strerror
Four, the file descriptor with the file pointer transformation
For system calls of file descriptor with the file pointer the ANSI C standard can be transformed using the following function:
1, fileno: to convert the file pointer to the file descriptor
2, fdopen: converts the file descriptor file pointer

Five, the file system calls
1, the open system call
(1) there are several ways to allow access to the file of file descriptors, the most commonly used is to use the open () system call,
2, the function prototype
Int the open (const char * path, Int flags);
Parameter
Path: the name of the file, and it can contain Path (absolute and relative)
Flags: the file open mode
The return value
Open the success, the file descriptor returned; Non-negative integer
Open the failure, return 1
Six,

  • Related