Experiment 4: File backup experiment

1. Purpose of the experiment

1. Familiar with the file and directory structure of the Linux file system
2. Master the basic features of the file system
3. Master common file operation functions.

2. Experimental content

Write a C program to simulate the simple I/O stream operation of the Linux file system: backup files, and back up the source file source.dat as target.dat file. Requirements:
1) Use C library function to realize file backup;
2) Use system call function to realize file backup

3. Experimental steps

File backup using C library functions

#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
        char buf;
        FILE *source, *backup;
        printf("This program backup file based on C Library.\n");
        //根据自己的路径和文件名称来改变
        if ((source = fopen("./source1.dat", "r"))==NULL)
        {
    
    
                printf("Error in opening file.\n");
                exit(1);
        }
        if ((backup = fopen("./target1.dat", "w"))==NULL)
        {
    
    
                printf("Error in creating file.\n");
                exit(1);
        }
        while (fread(&buf, sizeof(buf), 1, source) == 1)
        {
    
    
                if (!fwrite(&buf, sizeof(buf), 1, backup))
                {
    
    
                        printf("Error in writing file.\n");
                        exit(1);
                }
        }
        if (ferror(source) != 0)
        {
    
    
                printf("Error in reading file.\n");
                exit(1);
        }
        else
        {
    
    
                printf("Success in reading source file.\n");
        }
        if (fclose(source))
        {
    
    
                printf("Error in close file.\n");
                exit(1);
        }
        else
        {
    
    
                printf("Success in close source file.\n");
        }
        
        if (fclose(backup))
        {
    
    
                printf("Error in close file.\n");
                exit(1);
        }
        else
        {
    
    
                printf("Success in close target file.\n");
                exit(1);
        }
}

Use the Linux system to call file functions

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>


int main()
{
    
    
        char buf[256] = {
    
    0};
        char * path1 = "./source2.dat";   //根据自己的路径来做自己的改变哟
        char * path2 = "./target2.dat";

        int source, backup;
        if ((source = open(path1, O_RDONLY))==-1)
        {
    
    
                printf("Error in opening file.\n");
                exit(1);
        }
        if ((target = open(path2,  O_WRONLY | O_CREAT, 0600))==-1)
        {
    
    
                printf("Error in creating file.\n");
                exit(1);
        }
        int num = 0;
        while ( (num = read(source, buff, 256)) > 0)
        {
    
    
                write(target, buff, num);
        }
        printf("Success in reading source file\n");
        if (close(source) == -1)
        {
    
    
                printf("Error in close source file.\n");
                exit(1);
        }
        else
        {
    
    
                printf("Success in close source file.\n");
        }
        
        if (close(target) == -1)
        {
    
    
                printf("Error in close target file.\n");
                exit(1);
        }
        else
        {
    
    
                printf("success in close target file.\n");
                exit(1);
        }
}

4. Experimental results

C library function implementation results

insert image description here

insert image description here

insert image description here

Linux system call implementation

insert image description here

insert image description here

insert image description here

5. Experimental thinking

1. What is the principle of using system call functions open(), read(), write(), close() to realize simple file backup?

The system call functions open(), read(), write(), and close() are used to back up files through file descriptors. File descriptors are some values ​​used to describe the configuration of files. Through the management of file descriptors , open establishes an access path to the file or device. If the call is successful, it returns a file descriptor that can be used by other system calls such as read and write. The function of write is to write the first nbytes bytes of the buffer buf into the file associated with the file descriptor, and return the actual number of bytes written. The function of the read system call is to read nbytes bytes of data from the file related to the file descriptor, put them into the data area buf, and return the number of bytes read. The function of the close function is to close the association between the file descriptor and its corresponding file.

2. What is the principle of using the C library functions fopen(), fread(), fwrite(), fclose() to realize simple file backup?

In the linux system, files and devices are regarded as data streams. Before operation, the stream must be opened. A stream can be opened by calling the library function fopen(). The return value of the library function fopen() is a FILE structure pointer , which contains all the information needed to operate on the opened stream. After the stream operation is completed, operations such as clearing the buffer and saving data need to be performed, so at this time, the stream needs to be closed and the function fclose() is called to complete. In the process of implementing file backup, two streams are actually established, and the data in one stream is written into the other stream to achieve backup.

3. What is the difference between the above two?

insert image description here

Functions such as system calls open() and functions such as C library function fopen() are related to the operation of files, but the former runs in kernel mode and is based on file descriptors, while the latter It runs in user mode and is based on data streams, and each file stream corresponds to an underlying file descriptor. When calling functions such as the C library function fopen(), the corresponding operation will be implemented with the corresponding system call function when entering the kernel.

Guess you like

Origin blog.csdn.net/weixin_45788387/article/details/122031739