File i / o operations correlation function


Linux system files seen everything, Linux has 7 types of files: regular files - directory (dierectory) file, the symbol (link) link character (character) device file, block (block) device files and pipes (pipe ) files, sockets (socket) files. Which files, directories, symbolic links will take up disk space to store, and block device, character device, sockets, pipes is a pseudo-file, it does not take up disk space.

When faced with does not understand the function can be queried with the man command

The first part is the manual command man. 1 the printf man
man Manual 2nd part man 2 write system call is the
third part is a library function man pages man 3 printf

File manipulation functions described below are all part of system calls

Also to error information, call strerror () and perror ()

perror(“Oen  failure”);
printf(“Open  failure: %s\n”, strerror(errno));

Perror is simple to use, but we used the latter


A file descriptor

For the kernel, all open files, devices, network socket are referenced by file descriptor, a file descriptor is a non-negative integer. When you open an existing file or create a new file, the kernel returns a file descriptor to the process. When reading and writing a file using the file descriptor returned open or creat the document identifier, which is passed as a parameter to read or write.

When the application is running, the system will default to the process three open file descriptors:
standard input: STDIN_FILENO (0)
Standard Output: STDOUT_FILENO (1)
standard error: STDERR_FILENO (2)

File descriptors range is: 0 ~ OPEN_MAX-1


Second, the file i / o-correlation function

open()

int open(const char *path, int oflag, *mode*);

Return Value int fd file descriptor (file description), open system call returns a file descriptor must be minimal, the unused file descriptor values. I.e. 3 , 0 as standard input, standard output 1, 2 will open the default standard error.

Parameters:
1. * path: the path name of the file to be opened, the device
2 ** oflag:. ** ORed configuration options by a plurality of parameters oflag
Required: O_RDONLY (read-only), O_WRONLY (write only), O_RDWR (reading and writing)
optional: are appended to the end of the file when O_APPEND each write.
O_CREAT file does not exist it is created, this option requires the use of third parameter MODE
the O_NONBLOCK If a path is the FIFO, block, character special file This option opens this file and subsequent I / O operation setting non-blocking mode the way.
O_TRUNC If the file exists, but is write-only or read-write successfully opened, then taken to a length 0;
O_EXEC, O_SEARCH, O_CLOEXEC, the O_NOCTTY ...
3. MODE: When oflag with O_CREAT option must take this parameter is used to specify the file open the permission modes, such as 066.

  • Example: int fd;
  • fd = open(“text.txt”, O_RDWR|O_CREAT|O_TRUNC, 0666);
  • fd = open ( "text.txt", O_WRONLY | O_APPEND);
    where fd represents the file text.text

creat()

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

This function is used to create a new file and returns its fd. It is equivalent to
open (path, O_WRONLY | O_CREAT | O_TRUNC, mode);

  • 例: int fd;
    fd=creat(“text.txt”, 0644);

close()

int  close(int   fd);

This function is used to close an open file, but also the release of the process applied to all records of the file lock when you close a file. When a process terminates, the kernel automatically shut it all open files.

write()

 ssize_t   write(int fd,  const void *buf, size_t   nbytes);

The write success, the number of bytes actually written is returned, else return -1.

read()

ssize_t   read(fd,buf,sizeof(buf));

Description Returns 0 if fd is less than the read file error, can streror (errno) Get malposition
as read successfully, it returns the number of bytes read. As of the end of file has been reached, 0 is returned.

BUFF must be emptied before the Read
menset (buf, 0, the sizeof (BUFF);
Also, if using the first write () function, should file offset (i.e., the cursor position) adjusted at the beginning, as used herein lseek () function, the following introduction, or add O_APPEND when using open ()

lseek()

"Current file offset" open every file has associated with it. It is usually a non-negative integer, to measure the number of bytes from the beginning of the file. Read and write operations are usually starts from the current file offset, and use the number of bytes to read and write the offset increases.

According to the system default, when you open a file, unless you specify O_APPEND option, otherwise the offset is set to 0.

   off_t     lseek(int fd, off_t  offset, int  whence);

The whence:
SEEK_SET, then the file offset from the beginning of the file is set to offset bytes;
SEEK_CUR, then the offset of the file is set to the current value plus offset, offset may be positive or negative;
SEEK_END, then the file offset plus length is set to offset, offset can be positive or negative;

  • E.g
  • lseek (fd, 0, SEEK_END); file offset to the last byte of the file;

dup () and dup2 ()

int  dup(int  fd);
int dup2(int fd, int fd2);
  • New file descriptor dup minimum value must be returned file descriptor currently available.
  • dup2 fd2 parameter can specify the value of the new descriptor. If fd2 already open, then turn off. The fd equal to fd2, dup2 returns the fd2, without closing it.

E.g

 int     fd = -1;
 fd = open(“std.txt”, O_RDWR|O_CREAT|O_TRUNC, 0666);
 dup2(fd, STDOUT_FILENO);
 printf(“Hello Word!\n”);  //标准输出就会关闭,hello world会被输出重定向到文件fd即std.txt中

These two functions can be used to achieve dimensional input, standard output and standard error redirection

ioctl()

ioctl () function has the I / O operation glove box, other functions can not be represented in this chapter I / O operations can usually () represented by ioctl. Terminal I / O, device I / O is the most used where ioctl ().

int  ioctl(int  fd,  int cmd,  ...);

fd: file descriptor
cmd: command word, this parameter must be consistent with the device driver in cmd.
The third parameter is optional

stat()

These two functions are used to return information about a file or directory,
only stat () The first parameter is the file name,
and the first argument fstat () is the corresponding file descriptors open.

int stat(const char * restrict path, struct  stat *restrict buf);
int fstat(int fd, struct stat *buf);
  • E.g
  • struct stat stbuf; stat(“stat.c”, &stbuf);

access()

access can be used to test whether a file exists or test their permission bits:

int access(const char *path, int mode);
  • mode: F_OK、R_OK、W_OK、X_OK

Example 1 test whether a file exists:
IF ((Access ( "test.txt", F_OK))!) // return 0 means; return -1 means do not exist.
{
The printf ( "File exist \ n-");
}

Example 2 test whether the file read and write:
IF ((Access ( "test.txt", R_OK | W_OK))!) // return 0 means; return -1 means do not exist.
{
The printf ( "MODE IS File Read / Write \ n-");
}

unlink()

int unlink(const char *path);

The system call can be used to delete a file
Calling it will link to the file specified number of path minus 1, if there are other links to the file, you can still access the data in the file through other links.
Only when the link count reaches zero, the contents of the file are deleted before. If there is a process to open the file, its contents can not be deleted. When you close a file, the kernel first checks the number of processes open the file, if the count reaches zero, the kernel link to go check it count, if the count is 0, then delete the file content.

rename()

int  rename(const char *oldname, const char *newname);

Call this function can rename the file.


Third, the folder action

Create a folder:

int mkdir(const char *pathname, mode_t mode);

To delete a folder:
int rmdir (const char * pathname);

Open the folder:

DIR *opendir(const char *pathname);

Reading folder:

struct dirent * readdir(DIR *dp);

Close the folder:

int closedir(IDR *dp);

Change the working directory:

int chdir(const char * pathname);

Released two original articles · won praise 2 · Views 108

Guess you like

Origin blog.csdn.net/weixin_46027505/article/details/104325867