Linux (basic IO, file permissions, Makefile)

Table of contents

1. man manual

1.1 Chineseization

1.2 Specific use

2. File permissions

2.1 Understanding permissions

2.2 File detailed information query

2.3 Permission changes

3. Commonly used function interfaces

3.1 open

3.2 read

3.3 write

3.4 close

3.5 Function usage examples

4, make the Makefile

4.1 The difference between make and Makefile

4.2 Writing Makefile

5. Simple operation of vim


1. man manual

1.1 Chineseization

Install Chinese language package

sudo apt-get update
sudo apt-get install manpages-zh

1.2 Specific use

Use three methods to query the details of the open interface

//man -L zh_CN open
man 1 open
man 2 open
man 3 open

2. File permissions

2.1 Understanding permissions

In Ubuntu (and other UNIX-like operating systems), file permissions are used to control who can do what to a file. File permissions are represented as a set of three-digit permission strings in combinations of permission bits. Each permission string represents the permissions of the file's owner, group, and other users.  Detailed description of Ubuntu file permissions:

A permission string consists of nine characters, which can be divided into three groups of three characters each:

  • Group 1: File types and owner permissions
  • Second Group: Group Permissions
  • Group 3: Other user permissions

The three characters in each group represent read (r), write (w), and execute (x) permissions. The corresponding character is the corresponding letter if a permission bit is granted, or a minus sign (-) if it is not granted.

The following is an example of a permission string:-rwxr--r--

This permission string means:

  • File type: ordinary file (minus sign indicates)
  • Owner permissions: read, write, execute
  • Group permissions: read
  • Other user permissions: Read

2.2 File detailed information query

You can use the ll command to view detailed information about files in the current folder.

 Or view the specified file

zsd@Ubuntu:~/study/linuxStudy$  ls -l test.c
-rw-rw-r-- 1 zsd zsd 0  8月 27 15:34 test.c

In the above output, -rw-r--r-- is the permission string of the file. The first character - represents the file type (normal file), d represents hidden files, the next three characters are owner permissions, the next three characters are group permissions, and the last three characters are other user permissions.

2.3 Permission changes

To change file permissions, use the chmod command. For example, to set the owner permissions of the file "test.c" to read, write, and execute, you can execute the following command:

chmod 700 test.c

This will change the permissions string to -rwx------, meaning the owner has full permissions and other users have no permissions.

3. Commonly used function interfaces

3.1 open

openThe function is a system call used to open a file or create a new file, and returns a file descriptor (file descriptor) for subsequent read and write operations on the file. It is declared in the <fcntl.h> header file, and its function prototype is as follows:

#include <fcntl.h>
int open(const char *pathname, int flags, mode_t mode);

① open Parameters and functions of the function:

  • pathname(file path): This is a string representing the path to the file to open or create. Can be a relative path or an absolute path.

  • flags (flag bit) : This is an integer that specifies the method and options for opening the file. Common flags include:

    • O_RDONLY: Open for reading only.
    • O_WRONLY: Open for writing only.
    • O_RDWR: Open for reading and writing.
    • O_CREAT: If the file does not exist, create the file.
    • O_TRUNC: If the file exists, truncate the file (set the file size to 0).
    • O_APPEND: Append to the end of the file.

    These flags can be combined by bitwise OR (|) to meet specific needs. For example, O_RDONLY | O_CREAT means to open the file read-only and create it if it does not exist.

  • mode(File Permissions): This is an integer, used only when creating a file. It specifies the permissions of the newly created file (read, write, and execute permissions for the file owner, group, and other users). Usually expressed in octal, such as 0644, which means the owner can read and write, and other users can only read.

②Function return value:

open The return value of the function is an integer, representing the file descriptor. If the file is successfully opened or created, the returned file descriptor is a nonnegative integer. If an error occurs, the return value is -1, and you can use errno to get the specific error code.

3.2 read

readThe function is a system call used to read data from a file descriptor. It is declared in the <unistd.h> header file, and its function prototype is as follows:

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

① Parameters and functions of read function:

  • fd(file descriptor): This is an integer value indicating which file descriptor to read data from. A file descriptor is an integer value used to identify an open file, socket, etc. In the code, the open function is used to open the file, and the returned file descriptor is passed to the read function.

  • buf(buffer): This is a pointer to the memory buffer where the read data is stored. read The function will store the read data into this buffer.

  • count(Number of bytes read): This is the maximum number of bytes to read. The read function attempts to read up to count bytes of data from the file.

②Function return value:

readThe return value of the function is an integer of type ssize_t, representing the actual number of bytes read. The return value can be one of the following:

  • If the return value is positive: it indicates that the specified number of bytes was successfully read.
  • If the return value is 0: it means the end of file (EOF) has been reached.
  • If the return value is -1: an error occurred. The specific error code can be obtained by checking errno.

3.3 write

writeThe function is a system call used to write data to a file descriptor. It is declared in the <unistd.h> header file, and its function prototype is as follows:

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

①writeFunction parameters and functions:

  • fd(file descriptor): This is an integer value indicating which file descriptor to write data to. A file descriptor is an integer value used to identify an open file, socket, etc. In the code, you use the open function to open the file and pass the returned file descriptor to the write function.

  • buf(buffer): This is a pointer to the memory buffer containing the data to be written. write The function will write the data in the buffer to the file.

  • count(Number of bytes written): This is the number of bytes to write. The write function attempts to write the first count bytes of data in the buffer to the file.

②Function return value:

writeThe return value of the function is an integer of type ssize_t, representing the number of bytes actually written. The return value can be one of the following:

  • If the return value is non-negative: it indicates that the specified number of bytes was successfully written.
  • If the return value is -1: an error occurred. The specific error code can be obtained by checking errno.

3.4 close

closeThe function is a system call used to close the file descriptor. It is declared in the <unistd.h> header file, and its function prototype is as follows: 

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

①closeAnalysis of function:

  • fd(file descriptor): This is an integer value representing the file descriptor to close. A file descriptor is an integer value used to identify an open file, socket, etc. In your code, you might have used the open function to open the file and passed the returned file descriptor to the close function.

②Function return value:

close The return value of the function is an integer. If the file is closed successfully, the return value is 0. If an error occurs, the return value is -1, and the specific error code can be obtained by checking errno.

3.5 Function usage examples

#include <fcntl.h>   // 包含文件操作相关的头文件
#include <unistd.h>  // 包含系统调用相关的头文件
#include <stdio.h>   // 包含标准输入输出库,用于使用 perror 函数

int main() {
    // 打开源文件,只读方式
    int src_fd = open("source.txt", O_RDONLY);
    if (src_fd == -1) {
        perror("Error opening source file");
        return 1;
    }

    // 打开目标文件,写入方式(如果不存在则创建)
    int dest_fd = open("destination.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (dest_fd == -1) {
        perror("Error opening destination file");
        close(src_fd);  // 关闭源文件
        return 1;
    }

    // 读取源文件数据并写入目标文件
    char buffer[1024];
    ssize_t bytes_read;

    while ((bytes_read = read(src_fd, buffer, sizeof(buffer))) > 0) {
        ssize_t bytes_written = write(dest_fd, buffer, bytes_read);
        if (bytes_written == -1) {
            perror("Error writing to destination file");
            close(src_fd);   // 关闭源文件
            close(dest_fd);  // 关闭目标文件
            return 1;
        }
    }

    // 检查读取过程中是否出错
    if (bytes_read == -1) {
        perror("Error reading from source file");
        close(src_fd);   // 关闭源文件
        close(dest_fd);  // 关闭目标文件
        return 1;
    }

    // 关闭文件
    if (close(src_fd) == -1) {
        perror("Error closing source file");
        return 1;
    }

    if (close(dest_fd) == -1) {
        perror("Error closing destination file");
        return 1;
    }

    printf("File copy successful.\n");

    return 0;
}

4, make the Makefile

4.1 The difference between make and Makefile

make: make is a command line tool for automating the building of software projects. It checks the last modification date of source code files and determines which files need to be recompiled based on a set of rules and dependencies. make Widely used on Linux and UNIX-like systems to reduce the effort of manual compilation.

Makefile: Makefile is a text file that contains rules, dependencies, and commands for building software projects. It tells the make tool how to compile and link the source code files, and what operations need to be performed during the build process. Makefile Usually includes target, dependencies and commands to be executed.

4.2 Writing Makefile

# 编译器
# CC = g++
CC = gcc

# 编译选项
# CFLAGS = -Wall -std=c++11
# CFLAGS = -Wall -std=c++11 -g
# CFLAGS = -Wall -std=c++11 -static
CFLAGS = -Wall

# 目标文件名
TARGET = myprogram

# 所有的源代码文件
SOURCES = main.c utils.c mytest.c

# 生成目标文件的中间文件
OBJECTS = $(SOURCES:.c=.o)

# 默认目标
all: $(TARGET)

# 生成目标文件
$(TARGET): $(OBJECTS)
	$(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS)

# 编译每个源文件到目标文件
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# 调试目标
# debug: $(TARGET)
	# gdb ./$(TARGET)

# 清理生成的文件
clean:
	rm -f $(OBJECTS) $(TARGET)

5. Simple operation of vim

Enter: help to view help, press Ctrl + w 然后按下 q 退出帮助

  1. Start Vim: Enter the vim command in the terminal, followed by the file name to start Vim and open the specified file:

  2. Modal editing: Vim has several modes, the two main ones are:

    • Normal Mode: Used for navigation, deletion, copy and other operations.
    • Insert Mode: used to enter text.
  3. Changing model:

    • Enter normal mode: Press Esc key.
    • Enter insert mode: Press the i key (insert before the cursor), a key (insert after the cursor) or other command (such as o or O).
  4. Save Sum Exit:

    • Save the file: Press the : key in normal mode, then enter w and press the Enter key.
    • Save and exit: Press the : key in normal mode, then enter wq and press the Enter key .
    • Exit without saving: Press the : key in normal mode, then enter q! and press the Enter key .
  5. Basic movement:

    • Move up:k
    • Move down:j
    • Shift left:h
    • Shift right:l
  6. 删删和复法

    • Delete characters: Press the x key in normal mode.
    • Copy row: Press yy in normal mode.
  7. Undo and Redo:

    • Undo: Press the u key in normal mode.
    • Redo: Press the Ctrl + r keys in normal mode.
  8. 查找和交换

    • Find: Press the / key in normal mode, then enter what you want to find and press the Enter key.
    • Replace: Press the : key in normal mode, then enter %s/old/new/g for a global replacement.

Guess you like

Origin blog.csdn.net/qq_57594025/article/details/132523686