Linux Application Development (4): Linux File and Time Programming

Source: Internet
Author: User

I. chicken ribs

1. There are two methods for file programming in Linux:

(1) Linux system call

(2) c language library functions

The difference between the two lies in that (1) it depends on the Linux operating system, and (2) because it is a standard interface, the same program can run in different operating systems

 

Ii. File programming in Linux:

1. Create a file function: int creat (const char * filename, mode_t Mode)

Filename: the name of the file to be created. It must contain the path. The default value is the current path.

Mode_t mode: Creation Mode. Common modes include:


(1) s_irusr readable (2) s_iwurs writable (3) s_ixusr executable (4) s_irwxu readable writable executable, R, W, and X are file attributes in Linux, USR indicates user,

In addition to using the preceding macros, you can also directly use numbers to indicate File Access Permissions:

(1) 1: executable (2) 2: writeable (3) 4: readable, (4) Number and: Overall permission determination, eg: 3 = 1 + 2, therefore, it indicates writable executable (5) 0: no permission

2. file descriptor: in Linux, the system assigns a file descriptor to each open file. In essence, this file descriptor is a non-negative integer (like an ID card number, it is essentially a string of numbers, but it represents a much larger meaning than a number ). This string of numbers is allocated by the system. Its value range is 0 ~ Open_max: currently, the value of open_max in most systems is 1023, which allows a process to open 1024 files at the same time.

3. Open File function: (1) Open function with two parameters, int open (const char * pathname, int flags)


(2) Open functions with three parameters: int open (const char * pathname, int flags, mode_t Mode)

Parameter description:

Pathname: name of the file to be opened (path must be included. The default value is the current path)

Flags: Open flag, a common open flag


O_rdonly: Read Only, read-only,


O_wronly: Write only, open only in write mode,


O_rdwr: Read Write. The read/write mode is enabled,


O_append: open in append mode,


O_creat: Creates a file,


O_noblock: open in non-blocking mode

Take a look at the o_create flag. If we use the o_create flag to open a function, the function used is:

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

In this case, you need to specify the mode to indicate the object access permission. For the permission expression method, see 1. If there is no o_creat in flags, use two functions to open the function.

4. close the file: int close (int fd );

FD: file descriptor. FD is the file descriptor allocated by the system when the file is opened.

5. Read File function: int read (int fd, const void * Buf, size_t length );

How to use:


(1) read length bytes from the file defined by the file descriptor FD to the buffer zone pointed to by the Buf.


(2) the return value is the number of actually read bytes. (the actual result is that when we want to read length bytes from the FD file, but in fact there are only <length bytes in the file, the returned int is the number of bytes actually read)

6. File writing function: int write (int fd, const void * Buf, size_t length );

How to use:

(1) write the length of the buffer to which the Buf points to the file to which the FD points

(2) The returned value is the number of bytes actually written.

7. File locating function: int lseek (int fd, offset_t offset, int whence)

How to use:

(1) Move the file read/write pointer to the offset byte relative to the whence

(2) return value: the position of the file pointer relative to the file header.

(3) whence is a benchmark and the following values can be used:

Seek_set: the reference point is the file header.

Seek_cur: the reference point is the current position of the file pointer.

Seek_end: the reference point is the end of the file.

(4) When offset is negative, it indicates moving forward

8. File Access judgment: Sometimes we need to determine whether a file can perform certain operations (such as reading, writing, and executable). In this case, we can use the access function

Int access (const char * pathname, int Mode)

Pathname: File Name

Mode: the access permission to be judged. You can use the following permissions or their combinations.

(1) r_ OK, file readable (2) w_ OK, file writable (3) x_ OK, file executable (4) f_ OK, file exists

Return Value: The test is successful, and the function returns 0. Otherwise, if one of the conditions does not match,-1 is returned.

In summary, just like adding, deleting, modifying, and querying four operations in the data, the four operations in file operations are creation, read/write, location, and close. You can master these four operations.

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.