Linux File Programming

Source: Internet
Author: User
Tags types of functions
Linux File programming-general Linux technology-Linux programming and kernel information. The following is a detailed description. On the Linux platform, two types of functions can be used for file programming: (1) Linux operating system file API; (2) c language I/O library function. The former depends on Linux calls, and the latter is actually independent from the operating system, because in any operating system, the methods for operating files using C language I/O library functions are the same

Linux File API
The file operation API of Linux involves creating, opening, reading, writing, and closing files.

The open function has two forms, in which pathname is the name of the file we want to open (including the path name, which is considered to be under the current path by default ), flags can be used to combine the following values or values:

Logo meaning
O_RDONLY open a file in read-only mode
O_WRONLY open the file in write-only mode
O_RDWR opens a file in read/write mode
O_APPEND
O_CREAT creates a file
O_EXEC if O_CREAT is used and the file already exists, an error occurs.
O_NOBLOCK open a file in non-blocking mode
O_TRUNC if the file already exists, delete the file content

O_RDONLY, O_WRONLY, and O_RDWR can only use any one of them.

If the O_CREATE flag is used, the int open (const char * pathname, int flags, mode_t mode) function is used. In this case, we need to specify the mode flag, indicates the object access permission. Mode can be a combination of the following conditions:

Logo meaning
S_IRUSR users can read
S_IWUSR users can write
S_IXUSR users can execute
S_IRWXU users can read, write, and execute
S_IRGRP group readable
S_IWGRP group can be written
S_IXGRP group executable
S_IRWXG group can be read and written for execution
S_IROTH others can read
S_IWOTH others can write
S_IXOTH others can execute
S_IRWXO others can read, write, and execute
S_ISUID
S_ISGID: Set the execution ID of the group.

In addition to the above macro "or" logic to generate a flag, we can also use numbers to represent the various permissions of the file. Linux uses a total of five numbers to represent the permissions of the file: the first digit indicates the user ID, the second digit indicates the group ID, the third digit indicates the user's own permission, the fourth digit indicates the Group permission, and the last digit indicates the permissions of others. Each number can be 1 (Execution permission), 2 (write permission), 4 (read permission), 0 (none), or the sum of these values.

Read/write
After the file is opened, we can read and write the file. in Linux, the read and write Functions are called by the system:

Int read (int fd, const void * buf, size_t length );
Int write (int fd, const void * buf, size_t length );

The parameter buf is the pointer to the buffer, and the length is the buffer size (in bytes ). The read () function reads length bytes from the file specified by the file descriptor fd to the buffer zone pointed to by the buf. The returned value is the actual number of bytes read. Function write writes the length byte from the buffer to which the buf points to the file pointed by the file descriptor fd. the return value is the actual number of bytes written.

Positioning
For random files, we can read and write at random locations and use the following functions to locate them:

Int lseek (int fd, offset_t offset, int whence );

Lseek () moves the file read/write pointer to the offset byte relative to whence. When the operation is successful, the position of the returned file pointer relative to the file header. The whence parameter can use the following values:

SEEK_SET: Start of relative file
SEEK_CUR: Current Position of the relative file read/write pointer
SEEK_END: relative to the end of the file

The offset value can be a negative value. For example, the following call can move the file pointer 5 bytes forward from the current position:

Lseek (fd,-5, SEEK_CUR );

Because the return value of the lseek function is the position of the file pointer relative to the file header, the following returned values are the file length:

Lseek (fd, 0, SEEK_END );

Close
After the operation is complete, we need to close the file. We only need to call close. fd is the file descriptor we want to close:

Int close (int fd );

C language library functions
The file operations of C-library functions are actually independent from the specific operating system platform. These functions are used in DOS, Windows, Linux, and VxWorks:

Create and open
FILE * fopen (const char * path, const char * mode );

Fopen () is used to open the specified file filename. The mode is the open mode. The following table lists the open modes supported in C:

Logo meaning
R and rb are opened in read-only mode.
W and wb are opened in write-only mode. If the file does not exist, create the file. Otherwise, the file is truncated.
A and AB are opened in append mode. If the file does not exist, create the file.
R +, r + B, rb + Enabled in read/write mode
W +, w + B, wh + are opened in read/write mode. If the file does not exist, create a new file. Otherwise, the file is truncated.
A +, a + B, and AB + are opened in read and append mode. If the file does not exist, create a new file.

B is used to distinguish between binary files and text files, which are differentiated in DOS and Windows systems, but Linux does not distinguish between binary files and text files.

Read/write
The C-database functions support reading and writing files in a certain format in units of characters and strings. These functions are:

Int fgetc (FILE * stream );
Int fputc (int c, FILE * stream );
Char * fgets (char * s, int n, FILE * stream );
Int fputs (const char * s, FILE * stream );
Int fprintf (FILE * stream, const char * format ,...);
Int fscanf (FILE * stream, const char * format ,...);
Size_t fread (void * ptr, size_t size, size_t n, FILE * stream );
Size_t fwrite (const void * ptr, size_t size, size_t n, FILE * stream );

Fread () reads n fields from the stream, each of which is a size byte, and puts the read fields into the character array specified by ptr, returns the number of actually read fields. When the number of fields to be read is smaller than num, an error may occur during function calling or the end of the file. Therefore, you must call feof () and ferror () to determine.

Write () writes n fields from the array referred to by the buffer ptr to the stream. Each field is in size and returns the number of actually written fields.

In addition, the C-database functions provide positioning capabilities in the read/write process, including

Int fgetpos (FILE * stream, fpos_t * pos );
Int fsetpos (FILE * stream, const fpos_t * pos );
Int fseek (FILE * stream, long offset, int whence );
.

Close
Using the C library function to close a file is still a simple operation:

Int fclose (FILE * stream );

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.