Common File Operations in Linux, linux operations

Source: Internet
Author: User

Common File Operations in Linux, linux operations

Each system has applications for file operations, but they are roughly the same. In Linux, they also have file operations. Such as: file descriptor, open () function, close () function, read () function, write () function, file offset lseek () function, Get File status fstat () function, file space ing mmap () function, munmap () function that cancels mmap () ing, File Attribute fcntl () function, and file input/output control ioctl () function. This section mainly describes the open () function, close () function, read () function, and write () function.

I. file descriptor

The file descriptor is an integer, and all operations on the file are implemented through the file descriptor. In Linux, file descriptors are used to represent device files and common files. File descriptor is the hub connecting user space and kernel space in the file system. The file descriptor ranges from 0 ~ OPEN_MAX is a limited resource and needs to be released in time after use.

In Linux, there are three allocated file descriptors, namely standard input, standard output, and standard error. Their file descriptors are 0, 1, and 2, respectively.

2. open the open () and create () functions for creating files

Prototype of the open () function:

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

 

For open () functions, the header files sys/types. h, sys/stat. h, and fcntl. h must be included during use. After you open a file specified by pathname through the open () function, this function returns a value. If the file is successfully opened, an integer file descriptor is returned. -1 is returned when an error is returned. Pathname refers to a string variable. The length of this string variable is generally 1024 bytes. If the size is exceeded, the system automatically truncates and selects the first byte operation.

The flags variable is a file flag variable used to determine how the file is opened. The file can be opened in read-only, write-only, or read-write mode, which are represented by O_RDONLY, O_WRONLY, and O_RDWR respectively, you must use either of the three methods to open a file. In these three parameters, O_RDONLY is usually defined as 0, O_WRONLY is defined as 1, and O_RDWR is defined as 2.

In addition to the preceding three open methods, there are other parameters.

For example, O_APPEND: adds each write operation on the object to the end of the object.

O_CREAT: Creates a file if the file does not exist.

O_EXCL: Check whether the file exists. If yes,-1 is returned.

O_TRUNC: truncates the object length to 0. If the object exists and is successfully opened, the object length is truncated to 0.

O_NONBLOCK: open a file in non-blocking mode. The default mode is blocking.

1 # include <sys/types. h> 2 # include <sys/stat. h> 3 # include <fcntl. h> 4 # include <stdio. h> 5 int main (void) 6 {7 int fd =-1; 8 char filename [] = "test.txt"; 9 fd = open (filename, O_RDWR ); 10 if (fd =-1) 11 printf ("file opening failed !, Fd: % d \ n ", fd); 12 else 13 printf (" file % s opened successfully !, Fd: % d \ n ", filename, fd); 14 return 0; 15}

Ii. close the file close () function

The close () function closes an opened file and the resources occupied by the opened file.

Prototype of the close () function:

1 #include<unistd.h>2 int close(int fd);

The close () function disables a file descriptor. After the file descriptor is closed, it no longer points to any file, so that the file descriptor can be used again. 0 is returned when the function is successfully executed, and-1 is returned when an execution error occurs.

Example of the close () function:

1 # include <sys/types. h> 2 # inlcude <sys/stat. h> 3 # include <fcntl. h> 4 # include <unistd. h> 5 # include <stdio. h> 6 int mian (void) 7 {8 int fd = 0; 9 fd = open ("test.txt", O_RDONLY); 10 if (fd> 0) 11 printf ("the file is opened successfully !, Fd: % d \ n ", fd); 12 else 13 {14 printf (" file opening failed! \ N "); 15 exit (0); 16} 17 close (fd); 18 return 0; 19}

3. read the file read () function

The read () function reads data from open files. You can perform operations on the read data.

The prototype of the read () function:

1 #include<unistd.h>2 ssize_t read(int fd,void *buf,size_t count);

The read () function reads the count byte from the file corresponding to the file descriptor fd and stores it in the cache zone starting with the buf. If the count is 0, the read () function returns 0; if count is greater than SSIZE_MAX, the result is unpredictable. When the returned result of the read () function is-1, it indicates that the read function has an error. when the end of the file is reached, 0 is returned.

When using the read () function, count requests the number of bytes. The read () function may not be able to read so much data, in many cases, the actual number of bytes to be read is smaller than the number of bytes to be read.

For example:

(1) When reading a common file, the remaining bytes in the file are not enough bytes requested.

(2) When the device data is interrupted from the slave node, the default length is not enough to read the bytes requested by the read () function.

(3) When reading data from the network, the cache size may be smaller than the data size of the read request.

Example of the read () function:

1 # include <sys/types. h> 2 # include <sys/stat. h> 3 # include <fcntl. h> 4 # include <unistd. h> 5 # include <stdio. h> 6 int main (void) 7 {8 int fd =-1, I; 9 ssize_t size =-1; 10 char buf [10]; 11 char filenema [] = "test.txt"; 12 13 fd = open (filename, O_RDONLY); 14 if (fd =-1) 15 printf ("failed to open file % s !, Fd: % d \ n ", filename, fd); 16 else17 printf (" file % s opened successfully !, Fd: % d \ n ", filename, fd); 18 while (size) {19 size = read (fd, buf, 0); 20 if (size =-1) 21 {22 close (fd); printf ("An error occurred while reading the file! \ N "); 23} else {24 if (size> 0) {25 printf (" read % ld bytes: ", size); 26 for (I = 0; I <size; I ++) printf ("% c", * (buf + I); 27 printf ("\ n "); 28} else printf ("arrive at the end of the file \ n"); 29} 30} 31 close (size); 32 return 0; 33}

4. write a file write () function

The write () function writes data to open files and saves user data to files. Similar to the meaning of the read () function, the write () function writes data to the file descriptor fd. The data size is specified by count, and the buf is the pointer to write data, such as write () the Return Value of the function is the number of bytes of successfully written data. If the file fails to be written, the function returns-1.

The prototype of the write () function:

1 #include<unistd.h>2 ssize_t write(int fd,const void *buf,size_t count);

Example of the write () function:

1 # include <sys/types. h> 2 # include <sys/stat. h> 3 # include <fcntl. h> 4 # include <string. h> 5 # include <stdio. h> 6 # include <unistd. h> 7 int main (void) 8 {9 int fd =-1; 10 ssize_t size =-1; 11 char buf [] = "quick brown fox jump over the lazy dog"; 12 char filename [] = "test.txt"; 13 14 fd = open (filename, O_RDWR ); 15 if (-1 = fd) 16 printf ("failed to open file % s, fd: % d \ n", filename, fd ); 17 else 18 printf ("file % s opened successfully, fd: % d \ n", filename, fd); 19 size = write (fd, buf, strlen (buf )); 20 printf ("write % ld bytes to file % s \ n", size, filename); 21 close (fd); 22 return 0; 23}

 

 

 

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.