How to Talk about Linux: Basic file operations and examples

Source: Internet
Author: User
Tags function prototype readable

Basic concepts of file operations See blog: How to talk about Linux: underlying file I/O operations


1. Function description

  • open () function: is used to open or create a file, you can specify various parameters such as the properties of the file and the user's permissions when opening or creating the file.
    The so-called open file essentially establishes a connection between the process and the file, and the file descriptor uniquely identifies such a connection

  • Close () function: is used to close a file that has been opened. When a process terminates, all files that are opened by it are automatically closed by the kernel, and many programs use this feature to close a file without displaying it.

  • read () function: is used to put the data that is read from the specified file descriptor into the buffer and returns the number of bytes actually read in. If 0 is returned, there is no data to read, which means that the end of the file has been reached. The read operation starts at the current pointer position of the file. When the data is read from the terminal device file, it is usually read one line at a time.

  • Write () function: is used to write data to an open file, and the write operation starts at the current pointer position of the file. Writes to the disk file, the write () function returns failure if the disk is full or exceeds the length of the file.

  • Lseek () function: is to position the file pointer to the appropriate location. It can only be used in a location-accessible (random access) file operation. Pipelines, sockets, and most character device files are not located, so you cannot use Lseek () calls in the operations of these files.


2. Open () Function Syntax essentials
Required header File
#include <sys/types.h>/* provides the definition of type pid_t */
#include <sys/stat.h>/* Record file status header file */
#include <fcntl.h>/* file control, file access, read/write permission changes */
Function prototypes
int open (const char *pathname, int flags, int perms)

Before you manipulate a file, you must first open the file. The opening of the Open function is to associate the process files_struct struct with the file object files.

Function prototypes
int open (const char *pathname, int flags, int perms)

Pathname: The file name to be opened (including the pathname).

Flags: The way the file is opened (defined by one or more of the following constants or operands, defined in fcntl.h)
O_rdonly: Open File as read-only
O_wronly: Open File as Write-only
O_RDWR: Open file in read-write mode

O_creat: If the file does not exist, create a new file and set the permissions for it with the third parameter
O_EXCL: If the file already exists when using O_creat, an error message is returned. This parameter tests whether a file exists. At this point, open is an atomic operation that prevents multiple processes from creating the same file at the same time
O_noctty: When using this parameter, if the file is a terminal, this device is not assigned as the control terminal of this process
O_trunc: If the file already exists, all the original data in the file is deleted and the file size is set to 0.

O_append: Opens the file as added, while opening the file, the file pointer points to the end of the file, and the data to be written is added to the end of the file.
O_nonblock If pathname refers to a FIFO, a block special file, or a character special file, this option sets the non-blocking mode for this open operation and subsequent I/O operations for this file.
O_sync make each write wait until the physical I/O operation is complete

The flag parameter is available through the | "Composition, but the first 3 flags constants (O_rdonly, O_wronly, and O_RDWR) cannot be combined with one another.

Perms access to the created file (only used when creating a new file)

Can be defined with a set of macros: S_i (r/w/x) (Usr/grp/oth)
where r/w/x the read/write/execute permissions respectively
Usr/grp/oth indicates the file owner/file group/other user
Perms is the access permission for a file, either by means of a macro definition notation or by octal notation.
For example, S_IRUSR | S_IWUSR represents a read-write property that sets the owner of the file. Octal notation 600 also represents the same permissions
s_irwxu-set the file owner to have full permissions.

function return value

Success: Returns the file descriptor (the returned file descriptor must be the smallest unused descriptor number)
Failed:-1


3. Close () function Syntax essentials
Required header File
#include <unistd.h>
Function prototypes
int close (int fd)
function input Value
FD: File descriptor
function return value
0: Success
-1: Error


4. Read () Function Syntax essentials
Required header files: #include <unistd.h>
Function prototypes: ssize_t read (int fd, void *buf, size_t count)
Parameters:
FD: File descriptor
BUF: Pointer to the buffer that holds the read data
Count: Specifies the number of bytes read out
function return value
Success: Number of bytes read
0: End of file reached
-1: Error (such as when no data is readable on a file opened in nonblocking mode)

Attention:

    • There are a number of situations in which the number of bytes actually read is less than the required read:

    • When you read a normal file, you have reached the end of the file before you read the required number of bytes. For example, if you have 30 bytes before reaching the end of the file and require a read of 100 bytes, read returns 30, and the next time you call read, it returns 0 (the end of the file).

    • When reading from an end device, it is usually read one line at a time.

    • When reading from the network, the buffer mechanism in the network may cause the return value to be less than the number of bytes required to read.

    • Some record-oriented devices, such as tapes, return at most one record at a time.

    • The read operation starts at the current offset of the file and, after successful return, increases the number of bytes actually read.


5. Write () function syntax essentials
Required header files: #include <unistd.h>
Function Prototypes: ssize_t write (int fd, void *buf, size_t count)
Parameters:
FD: File descriptor
BUF: Pointer to the buffer that holds the data to be written
Count: Specify the number of bytes you want to read out
function return value
Success: Number of bytes written
Error:-1
When writing a normal file, the write operation starts at the current pointer position of the file.


6. Lseek () Function Syntax essentials
Each open file has a "current file offset" associated with it. It is a nonnegative integer, the number of bytes that should be computed at the beginning of the file, typically, the read and write operations start at the current file offset, and the amount of displacement increases the number of bytes read or written. System default: When a file is opened, the offset is set to 0 unless the o_append selection is specified.
Required header File
#include <unistd.h>
#include <sys/types.h>
Function prototypes: off_t lseek (int fd, off_t offset, int whence)
Parameters
FD: File descriptor
Offset: Offsets, distances that need to be moved, units of bytes, can be positive negative (move forward, move backward)

Function prototype off_t lseek (int fd, off_t offset, int whence)

Parameters:
whence: Base Position

    • Seek_set: The base position is the beginning of the file and the new position is the size of the offset

    • Seek_cur: The base position is the current position of the file pointer, and the new position is the current position plus the offset

    • Seek_end: The base position is the end of the file, the new position is the size of the file plus the size of the offset
function return value
success: The current displacement of the file, which is used for the next read or write operation.
Error: -1
The Lseek operation does not cause any I/O operations, just modifies the records in the kernel (the F_pos field of the open file object is modified)


7. Example Basic functions:
Read the last 10KB data from a file (source file) and go to another file (destination file);
In the instance, the source file is opened as read-only;
The target file is opened in write-only mode (can be read and write);
If the target file does not exist, you can create and set the initial value of the permission to 644, that is, the file owner is readable and writable, and the file belongs to the group and other users can read only.

/* Copy the latter 10 bytes of data from the file Src_file to the Dest_file file, provided the data is ready in Src_file */#include <unistd.h> #include <sys/types.h > #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #define Buffer_size 15//Read/write cache size, impact efficiency */#define SRC_FILE_NAME "src_file"/* source file name */#define DEST_FILE_NAME "Dest_file"/* Target filename text Name */#define OFFSET 10/* Copy data size */int main () {int src_file, dest_file;unsigned char buff[buffer_size];int real_read_len;/    * Open source file as read-only */src_file = open (Src_file_name, o_rdonly); if (Src_file < 0) {printf ("Src_file Open error\n"); exit (1);} /* Open the destination file as write-only, if the file does not exist, create the file, and the access permission value is 644 */dest_file = open (dest_file_name,o_wronly| o_creat,s_irusr| s_iwusr| s_irgrp| S_iroth); if (Dest_file < 0) {printf ("Dest_file_name Open error\n"); exit (1);} /* Move the read/write pointer of the source file to the starting position of the last 10KB */lseek (Src_file,-offset, seek_end);/* Reads the last 10KB data from the source file and writes it to the destination file, each reading and writing to 1KB */while (real_read_  Len = Read (src_file, buff, sizeof)) > 0) {write (dest_file, buff, real_read_len); }close (Dest_file); close (src_file); return 0;} 


First write the source file, enter a string of characters in the source file: (greater than 10 bytes)


Compile the Run source program in the terminal:



Use the Cat command in the terminal to output the contents of the destination file:


Print the variable Real_read_len, which shows 10 bytes:


As the reader can see, only 9 bytes of content are copied from the source file to the target file, and I think the Terminator EOF at the end of the file takes up one byte, so that only 9 bytes of data are displayed, and the file terminator EOF is non-printable and not displayed.

I have a shallow knowledge, if you know the reasons, but also hope to enlighten.

How to Talk about Linux: Basic file operations and examples

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.