Linux system Programming-system invoke I/O operations (file operations)

Source: Internet
Author: User

File Descriptor

in the world of Linux, all devices are files . We can call the function of I/O in the system (i:input, input; o:output, output), and operate the file accordingly ( open (), close (), write (), read (), etc. ).


When you open an existing file or create a new file, the system (kernel) returns a file descriptor that specifies the file that was opened. This file descriptor is equivalent to the label of the open file, the file descriptor is a non-negative integer, is the identity of the file, the operation of this file descriptor is equivalent to the operation of the descriptor specified by the file .


after the program runs (each process) has a table of file descriptors, standard input, standard output, standard error output device files are opened, corresponding file descriptors 0, 1, 2 are recorded in the table. These three file descriptors are opened by default when the program is running .

#define STDIN_FILENO 0 //file descriptor for standard input

#define STDOUT_FILENO 1 //file descriptor for standard output

#define STDERR_FILENO 2 //Standard error file descriptor


When you open other files after the program has run, the system returns the smallest available file descriptor in the File descriptor table and records the file descriptor in the table. A process in Linux can only open nr_open_default (that is, 1024) files, so you should call the close () function to close a file when it is no longer in use.


Common i/0 functions

Required header Files :

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>


int open (const char *pathname, int flags, mode_t mode);

features :

Open the file and create if the file does not exist.

Parameters :

Pathname: The path and filename of the file.

flags: A behavior flag that opens a file, such as read-only (o_rdonly, the first letter is not a data), to read or write new files (o_rdwr| O_creat) Open.


mode: This parameter, only valid when the file does not exist, refers to the permission to specify a file when creating a new file (click this link for file permission details).


return value :

Success: Successful return of open file descriptor

Failed:-1


int close (int fd);

features :

Close a file that is already open

Parameters :

FD: File descriptor, return value of open ()

return value :

Success: 0

Failed:-1


ssize_t Write (int fd, const void *ADDR, size_t count);

features :

Writes a specified number of data to a file (FD)

Parameters :

FD: File descriptor

addr: Data first Address

count: The length of the Write data (in bytes), in general, how much data, how much to write to the file, no more or less

return value :

Success: The number of bytes actually written to the data

Failed:-1


ssize_t Read (int fd, void *addr, size_t count);

features :

Reads the specified number of data into memory (buffer)

Parameters :

FD: File descriptor

addr: Memory Header Address

Count: number of bytes read

return value :

Success: Number of bytes actually read

Failed:-1


Practical Examples

Next, we use the above 4 system call I/O function to write a program, can realize the function like System command CP:



Use open () to open the source file, read the data from the file using read (), write the data to the destination file using write (), the sample code is as follows:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int main (int ARGC, Char *argv[]) {if ((argc = = 3) && (strcmp (argv[1], argv[2])! = 0)) {//guaranteed to have 3 parameters, and the source file and destination file name cannot be the same int fd_src, FD _dest, ret;//read-only open source file Fd_src = open (argv[1], o_rdonly); if (Fd_src < 0) {perror ("open argv[1]"); return-1;} New destination File Fd_dest = open (argv[2], o_wronly| O_creat, 0755); if (Fd_dest < 0) {close (FD_SRC);p error ("Open argv[2]"); return-1;} Do{char buf[1024] = {0};//read data from source file ret = read (FD_SRC, buf, sizeof (BUF));//write the data to the destination file, note the last parameter, how much write (Fd_dest, buf, R ET);} while (Ret > 0);//Close the Open file closed (FD_SRC); close (fd_dest);} return 0;}

The results of the operation are as follows:



For this tutorial sample code download please click here.

Linux system Programming-system invoke I/O operations (file operations)

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.