Linux system programming files

Now learn the second stage of embedded-Linux system programming. For system programming, there are still many things to take notes and record them.
1. File programming (how to use code to manipulate files to realize automatic execution of file creation, opening, editing, etc.)
Windows manually open files to
open/create files-----edit documents-----save documents-----close Document.
Linux is the same.

The operating system provides a lot of API
Linux system:
open open
read write write/read
cursor positioning lseek
close close

Open/create file Insert picture description here
pathname: the name of the file to be opened (including the path, the default is the current path)
Flags:
O_RDONLY read-only open O_WRONLY write-only open O_RDWR read and write open

When we attach permissions, open files can only be operated in accordance with this permission.
Only one of the above three constants should be specified, and the following constants are optional.
O_CREAT If the file does not exist, create it. When using this option, you need to specify the third parameter mode at the same time, which indicates the access permission of the new file.
O_EXCL If OCREAT is also specified and the file already exists, an error occurs, and the file fails to open (the return value is -1).
O_APPEND is added to the end of the file every time it is written. When opening a file with the
O_TRUNC attribute, if the file originally contains content, and it is read-only or write-only successfully opened, its length will be truncated to 0;
Mode: O_CREAT must be used in flags , and mode is recorded Access permissions for the created file.

1. Open the file : open the return value is of type int (file descriptor) to open the file successfully is a small integer greater than zero return (not negative) , if the open fails it will return a negative integer number.

int open(const char *pathname,int flags);

Insert picture description here
int fd; //Define an int type to receive the return value of open.
fd = open("./file name",O_RDWR); //Now this is readable and writable or can be opened with O_RDONLY for read-only and O_WRONLY for write-only.
If fd> 0, open the file successfully. If fd <0, open the file and fail.

**2. Open/create file: **Maybe there is no such file name. We can create a file if we cannot open the file.

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

//The last parameter is permission. We can view the file through ls -l -rwx, - (ordinary file), r (readable), w (writeable), x (executable)

Insert picture description here

If there is no such file name, create the file r = 4 (readable), w = 2 (writeable) x = 1 (executable)
fd = open("./file name", o_RDWR|O_CREAT, 0600);// 6 in 0600 is 4+2 (readable and writable)

3. Write to the file:
Insert picture description here
* fd: file descriptor (the value returned by open) void buf: an untyped pointer (content to be written in) size_t count: the size of the file to be written.

Insert picture description here
First, we open the file. If we find that there is no such file, we create the file, and then we write the sentence "Welcome to C" to the file. After writing, we close (fd) means to close the file and
write it in: write(fd,p,sizeof§); //This is written in but the char* pointer in Linux is 8 bytes. So I can't let it go. We should change sizeof§ to strlen§ the size of the length of p.

4. Read the file:
Insert picture description here

ssize_t read(int fd,void *buf,size_t count);

Reading success returns the number of bytes read. Reading failure returns 0. Reading error returns -1.
fd: file descriptor (the value returned by open) void *buf: an untyped pointer (content to be read) ) Size_t count: read the size of the file content.
Insert picture description here
**Note:** When we open the file and write the content, we should close the file and then reopen the file to read the file. Because if you don't close the file, the cursor is at the end and no data can be read.
For readP, I give it as much space as I write to it to prevent insufficient space.

5. File "cursor" position:
Insert picture description here

off_t lseek(int fd,off_t offset,int whence);

Move the file read and write pointer relative to whyce by offset bytes

Parameters The whence:
{ SEEK_SET - cursor to the file header SEEK_CUR - cursor to the end of the file SEEK_END - the cursor current position } of off_t offset: offset. Before we opened the file and wrote the content, we don't need to close and open the file and then read it, we just use lseek to move the cursor to the head. ** If it is then that the cursor is in the file header offset of 0 to change. **The whole number goes backward, and the negative number goes forward.





Insert picture description here
off_t lseek(int fd,0,SEEK_SET);

6. Use lseek to calculate the size:
Insert picture description here
use the return value of lseek to calculate the content size.

7. Create a file creat function:
Insert picture description here

int creat(const char * filename,mode_t mode)

**filename:** The name of the file to be created (including the path, the default is the current path)
**mode:** Creation mode // Readable and writable executable
macro definition number
S_IRUSR 4 Readable
S_IWUSR 2 Writable
S_IXUSR 1 Executable
S_IRWXU 7 readable, writable, executable

This is similar to open.

Guess you like

Origin blog.csdn.net/weixin_47457689/article/details/107502099