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

Source: Internet
Author: User

One, the 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

The above 3 constants are defined in /usr/include/unistd.h .


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.



second, commonly used i/0 function

Required Header files:

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>

int Open (const char *pathname, intflags, mode_tmode); Features:

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

Parameters:

Pathname: The path and filename of the file.

flags: Open the Behavior flags for the file, such as read-only (o_rdonly, the first letter is not 0 ), to read or write, or to create a new How to 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 (intFD); 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 head 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: the number of bytes actually read
failed: -1


third, the actual combat chapter

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



The instance code is as follows:

#include <stdio.h> #include <unistd.h> #include <string.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 as int fd_src, fd_dest, len;//read-only mode to 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};//reads data from the source file Len = Read (fd_src, buf, sizeof (BUF));//write the data to the destination file, note the last parameter, how much write (Fd_dest, buf, L EN);} while (len > 0);//Close the Open file closed (FD_SRC); close (fd_dest);} return 0;}

Operation Result:



SOURCE Download: http://download.csdn.net/download/lianghe_work/8830527


Transferred from: http://blog.csdn.net/tennysonsky/article/details/45868273






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.