操作系统:实验六文件操作实验

news2024/9/25 8:21:44

一、实验目的

1、了解文件系统功能及实现原理。

2、掌握LINUX下文件操作的有关系统调用。

3、熟悉main函数带参数运行的有关操作过程。

4、通过模拟程序实现简单的一级文件系统或二级文件系统。

、实验内容

1、编程显示文件自身。(1分)

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

void main(int argc,char *argv[])

{

    int p;

    char ch;

    if(argc!=2)

    {

        printf("you forget to enter a filename\n");

        exit(0);

    }

    if((p=open(argv[1],O_RDONLY))==-1) //以只读方式打开文件

  {

        printf("File open failare\n");

        exit(0);

    }

    while(read(p,&ch,1)==1) //每次读一个字符到ch

        printf("%c",ch);

    close(p);  //关闭文件

}

运行结果截屏:(包含编译、运行命令)

2.编程实现文件加密和解密(要求用系统功能调用实现文件的打开以及读写操作,并用main函数带参数方式编写程序)。(4分)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <string.h> // 添加这个头文件



#define XOR_KEY 'a'



void encrypt_decrypt(const char *input_file, const char *output_file)

{

    int input_fd, output_fd;

    char ch;



    // 打开输入文件

    if ((input_fd = open(input_file, O_RDONLY)) == -1) {

        fprintf(stderr, "无法打开输入文件: %s\n", input_file);

        exit(1);

    }



    // 创建输出文件

    if ((output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {

        fprintf(stderr, "无法创建输出文件: %s\n", output_file);

        close(input_fd);

        exit(1);

    }



    // 逐字符读取输入文件,进行加密/解密操作,并写入到输出文件

    while (read(input_fd, &ch, 1) == 1) {

        ch ^= XOR_KEY; // 与密钥 'a' 进行异或操作

        if (write(output_fd, &ch, 1) != 1) {

            fprintf(stderr, "写入输出文件失败: %s\n", output_file);

            close(input_fd);

            close(output_fd);

            exit(1);

        }

    }



    // 关闭文件描述符

    close(input_fd);

    close(output_fd);

}



int main(int argc, char *argv[])

{

    if (argc != 4) {

        fprintf(stderr, "用法: %s <模式> <输入文件> <输出文件>\n", argv[0]);

        fprintf(stderr, "模式: -e 加密, -d 解密\n");

        return 1;

    }



    const char *mode = argv[1];

    const char *input_file = argv[2];

    const char *output_file = argv[3];



    if (strcmp(mode, "-e") == 0 || strcmp(mode, "-d") == 0) {

        encrypt_decrypt(input_file, output_file);

    } else {

        fprintf(stderr, "无效的模式: %s\n", mode);

        return 1;

    }



    return 0;

}

检查命令行参数:

程序首先检查命令行参数的数量是否正确(应该是4个参数,包括程序名),并验证模式参数是否为加密(-e)或解密(-d)。

打开文件:

使用系统调用 open 打开输入文件,并以只读模式读取。如果文件无法打开,则打印错误信息并退出程序。随后,创建或重写输出文件。

读取和处理文件内容:

使用 read 系统调用逐字符读取输入文件内容,并对每个字符执行异或操作(与密钥 'a' 进行异或)。然后将处理后的字符写入输出文件。

关闭文件:

在处理完所有文件内容后,关闭输入和输出文件,以释放资源。

加密/解密逻辑

异或操作(XOR):c

复制代码ch ^= XOR_KEY;

异或操作是一个对称的加密操作:同一个操作可以用于加密和解密。例如,字符 'a' 异或 'a' 会返回0,任何字符 c 异或 'a' 两次会返回原字符 c。

3.编程实现文件的合并。(要求用系统功能调用实现文件的打开及操作,并用main 函数带参数形式编写程序)

1)以写方式打开file1,以读方式打开file2文件,利用lseek()将file1的指针定位到文件尾部,再将file2写入到file1,实现文件合并。(1分)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>



void merge_files(const char *file1, const char *file2)

{

    int fd1, fd2;

    char buffer[1024];

    ssize_t bytes_read, bytes_written;



    // 以写方式打开 file1

    if ((fd1 = open(file1, O_WRONLY | O_APPEND)) == -1) {

        fprintf(stderr, "无法打开文件: %s\n", file1);

        exit(1);

    }



    // 以读方式打开 file2

    if ((fd2 = open(file2, O_RDONLY)) == -1) {

        fprintf(stderr, "无法打开文件: %s\n", file2);

        close(fd1);

        exit(1);

    }



    // 读取 file2 并写入到 file1

    while ((bytes_read = read(fd2, buffer, sizeof(buffer))) > 0) {

        bytes_written = write(fd1, buffer, bytes_read);

        if (bytes_written != bytes_read) {

            fprintf(stderr, "写入文件失败: %s\n", file1);

            close(fd1);

            close(fd2);

            exit(1);

        }

    }



    // 关闭文件描述符

    close(fd1);

    close(fd2);

}



int main(int argc, char *argv[])

{

    if (argc != 3) {

        fprintf(stderr, "用法: %s <file1> <file2>\n", argv[0]);

        return 1;

    }



    const char *file1 = argv[1];

    const char *file2 = argv[2];



    merge_files(file1, file2);



    return 0;

}

文件打开方式:

在合并文件时,需要以写模式打开第一个文件(file1)以及以读模式打开第二个文件(file2)。

打开文件时,使用了 open() 系统调用,并传递了相应的参数来指定打开文件的方式(只读、只写、追加等)。

定位文件指针:

在将文件 file1 的指针定位到文件末尾时,使用了 lseek() 系统调用,这样可以确保在写入时数据会追加到文件末尾。

lseek() 函数的第三个参数用于指定偏移量,这里使用 SEEK_END 表示从文件末尾开始计算偏移量。

文件内容拷贝:

读取文件内容并将其拷贝到另一个文件时,使用了 read() 和 write() 系统调用。

read() 函数用于从文件中读取数据,将数据存储到缓冲区中。

write() 函数用于将数据从缓冲区写入到文件中。

在读取和写入过程中,使用了循环来处理文件内容,确保了可以处理大于缓冲区大小的文件。

2)以读方式打开file1、file2文件,以写方式打开file3文件,实现文件合并。(1分)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>



void merge_files(const char *file1, const char *file2, const char *file3)

{

    int fd1, fd2, fd3;

    char buffer[1024];

    ssize_t bytes_read, bytes_written;



    // 以读方式打开 file1

    if ((fd1 = open(file1, O_RDONLY)) == -1) {

        fprintf(stderr, "无法打开文件: %s\n", file1);

        exit(1);

    }



    // 以读方式打开 file2

    if ((fd2 = open(file2, O_RDONLY)) == -1) {

        fprintf(stderr, "无法打开文件: %s\n", file2);

        close(fd1);

        exit(1);

    }



    // 以写方式打开 file3

    if ((fd3 = open(file3, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {

        fprintf(stderr, "无法打开文件: %s\n", file3);

        close(fd1);

        close(fd2);

        exit(1);

    }



    // 从 file1 读取内容并写入到 file3

    while ((bytes_read = read(fd1, buffer, sizeof(buffer))) > 0) {

        bytes_written = write(fd3, buffer, bytes_read);

        if (bytes_written != bytes_read) {

            fprintf(stderr, "写入文件失败: %s\n", file3);

            close(fd1);

            close(fd2);

            close(fd3);

            exit(1);

        }

    }



    // 从 file2 读取内容并写入到 file3

    while ((bytes_read = read(fd2, buffer, sizeof(buffer))) > 0) {

        bytes_written = write(fd3, buffer, bytes_read);

        if (bytes_written != bytes_read) {

            fprintf(stderr, "写入文件失败: %s\n", file3);

            close(fd1);

            close(fd2);

            close(fd3);

            exit(1);

        }

    }



    // 关闭文件描述符

    close(fd1);

    close(fd2);

    close(fd3);

}



int main(int argc, char *argv[])

{

    if (argc != 4) {

        fprintf(stderr, "用法: %s <file1> <file2> <file3>\n", argv[0]);

        return 1;

    }



    const char *file1 = argv[1];

    const char *file2 = argv[2];

    const char *file3 = argv[3];



    merge_files(file1, file2, file3);



    return 0;

}

打开文件:

使用 open() 系统调用打开文件,需要指定文件名和打开模式(读、写、追加等)。

打开文件时,通常会检查返回值是否为 -1,这表示打开文件失败。

文件读取:

使用 read() 系统调用从文件中读取数据,需要提供文件描述符、存储数据的缓冲区以及要读取的字节数。

read() 返回实际读取的字节数,如果返回值为 0,表示已到达文件末尾。

文件写入:

使用 write() 系统调用将数据写入到文件中,需要提供文件描述符、数据缓冲区以及要写入的字节数。

write() 返回实际写入的字节数,如果返回值与要求写入的字节数不相等,可能表示写入失败。

关闭文件:

使用 close() 系统调用关闭文件描述符,以释放文件资源并确保写入的数据被刷新到磁盘上。

在程序结束前,应当关闭所有打开的文件描述符,以避免资源泄露和文件损坏。

3)以附加方式打开file1,以读方式打开file2,实现文件合并。(1分)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>



void merge_files(const char *file1, const char *file2)

{

    int fd1, fd2;

    char buffer[1024];

    ssize_t bytes_read, bytes_written;



    // 以附加方式打开 file1

    if ((fd1 = open(file1, O_WRONLY | O_APPEND)) == -1) {

        fprintf(stderr, "无法打开文件: %s\n", file1);

        exit(1);

    }



    // 以读方式打开 file2

    if ((fd2 = open(file2, O_RDONLY)) == -1) {

        fprintf(stderr, "无法打开文件: %s\n", file2);

        close(fd1);

        exit(1);

    }



    // 将 file1 的指针定位到文件末尾

    if (lseek(fd1, 0, SEEK_END) == -1) {

        fprintf(stderr, "无法将文件指针定位到文件末尾: %s\n", file1);

        close(fd1);

        close(fd2);

        exit(1);

    }



    // 从 file2 读取内容并附加到 file1 的末尾

    while ((bytes_read = read(fd2, buffer, sizeof(buffer))) > 0) {

        bytes_written = write(fd1, buffer, bytes_read);

        if (bytes_written != bytes_read) {

            fprintf(stderr, "写入文件失败: %s\n", file1);

            close(fd1);

            close(fd2);

            exit(1);

        }

    }



    // 关闭文件描述符

    close(fd1);

    close(fd2);

}



int main(int argc, char *argv[])

{

    if (argc != 3) {

        fprintf(stderr, "用法: %s <file1> <file2>\n", argv[0]);

        return 1;

    }



    const char *file1 = argv[1];

    const char *file2 = argv[2];



    merge_files(file1, file2);



    return 0;

}

4.编程实现一个简单的文件系统,要求实现以下功能:

文件创建    文件解密   文件显示    文件列目录  文件删除   

文件加密    文件合并    文件查询    文件复制    文件重命名  (选做题)     

注:

  1. 选做该题的同学,不用做前面的题,直接提交这一道题的程序和结果即可,成绩直接给优秀。
  2. 要求用系统功能调用实现文件的打开及操作。
  3. 要求列出程序编译、运行过程以及执行结果截屏。
  4. 结果截屏要求给出至少5个结果(比如,创建、加密、解密、合并、复制等)。
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <fcntl.h>

#include <dirent.h>



#define MAX_NAME_LENGTH 256

#define XOR_KEY 'a'



// 创建文件

void create_file(const char *filename) {

    int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

    if (fd == -1) {

        perror("无法创建文件");

        return;

    }

    close(fd);

    printf("文件 %s 已创建\n", filename);

}



// 删除文件

void delete_file(const char *filename) {

    if (unlink(filename) == -1) {

        perror("无法删除文件");

        return;

    }

    printf("文件 %s 已删除\n", filename);

}



// 复制文件

void copy_file(const char *source, const char *destination) {

    int src_fd, dest_fd;

    char buffer[1024];

    ssize_t bytes_read, bytes_written;



    src_fd = open(source, O_RDONLY);

    if (src_fd == -1) {

        perror("无法打开源文件");

        return;

    }



    dest_fd = open(destination, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

    if (dest_fd == -1) {

        perror("无法创建目标文件");

        close(src_fd);

        return;

    }



    while ((bytes_read = read(src_fd, buffer, sizeof(buffer))) > 0) {

        bytes_written = write(dest_fd, buffer, bytes_read);

        if (bytes_written != bytes_read) {

            perror("写入文件失败");

            close(src_fd);

            close(dest_fd);

            return;

        }

    }



    printf("文件 %s 已复制为 %s\n", source, destination);



    close(src_fd);

    close(dest_fd);

}



// 重命名文件

void rename_file(const char *filename, const char *newname) {

    if (rename(filename, newname) == -1) {

        perror("无法重命名文件");

        return;

    }



    printf("文件 %s 已重命名为 %s\n", filename, newname);

}



// 文件解密

void decrypt_file(const char *input_file, const char *output_file) {

    int input_fd, output_fd;

    char ch;



    if ((input_fd = open(input_file, O_RDONLY)) == -1) {

        perror("无法打开输入文件");

        return;

    }



    if ((output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {

        perror("无法创建输出文件");

        close(input_fd);

        return;

    }



    while (read(input_fd, &ch, 1) == 1) {

        ch ^= XOR_KEY;

        if (write(output_fd, &ch, 1) != 1) {

            perror("写入输出文件失败");

            close(input_fd);

            close(output_fd);

            return;

        }

    }



    close(input_fd);

    close(output_fd);



    printf("文件解密完成\n");

}



// 文件加密

void encrypt_file(const char *input_file, const char *output_file) {

    int input_fd, output_fd;

    char ch;



    if ((input_fd = open(input_file, O_RDONLY)) == -1) {

        perror("无法打开输入文件");

        return;

    }



    if ((output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {

        perror("无法创建输出文件");

        close(input_fd);

        return;

    }



    while (read(input_fd, &ch, 1) == 1) {

        ch ^= XOR_KEY;

        if (write(output_fd, &ch, 1) != 1) {

            perror("写入输出文件失败");

            close(input_fd);

            close(output_fd);

            return;

        }

    }



    close(input_fd);

    close(output_fd);



    printf("文件加密完成\n");

}



// 文件合并

void merge_files(const char *file1, const char *file2, const char *output_file) {

    int input_fd1, input_fd2, output_fd;

    char ch;



    if ((input_fd1 = open(file1, O_RDONLY)) == -1) {

        perror("无法打开输入文件");

        return;

    }



    if ((input_fd2 = open(file2, O_RDONLY)) == -1) {

        perror("无法打开输入文件");

        close(input_fd1);

        return;

    }



    if ((output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {

        perror("无法创建输出文件");

        close(input_fd1);

        close(input_fd2);

        return;

    }



    while (read(input_fd1, &ch, 1) == 1) {

        if (write(output_fd, &ch, 1) != 1) {

            perror("写入输出文件失败");

            close(input_fd1);

            close(input_fd2);

            close(output_fd);

            return;

        }

    }



    while (read(input_fd2, &ch, 1) == 1) {

        if (write(output_fd, &ch, 1) != 1) {

            perror("写入输出文件失败");

            close(input_fd1);

            close(input_fd2);

            close(output_fd);

            return;

        }

    }



    close(input_fd1);

    close(input_fd2);

    close(output_fd);



    printf("文件合并完成\n");

}



// 查看文件内容

void view_file(const char *filename) {

    int fd = open(filename, O_RDONLY);

    if (fd == -1) {

        perror("无法打开文件");

        return;

    }



    char buffer[1024];

    ssize_t bytes_read;



    while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {

        if (write(STDOUT_FILENO, buffer, bytes_read) != bytes_read) {

            perror("写入标准输出失败");

            close(fd);

            return;

        }

    }



    close(fd);

    printf("\n文件 %s 内容显示完毕\n", filename);

}



// 查看当前目录下的文件

void list_files() {

    DIR *d;

    struct dirent *dir;

    d = opendir(".");



    if (d) {

        printf("当前目录下的文件和目录:\n");

        while ((dir = readdir(d)) != NULL) {

            printf("%s\n", dir->d_name);

        }

        closedir(d);

    } else {

        perror("无法打开当前目录");

    }

}



// 主函数

int main() {

    char choice;

    char filename[MAX_NAME_LENGTH];

    char newname[MAX_NAME_LENGTH];

    char input_file1[MAX_NAME_LENGTH], input_file2[MAX_NAME_LENGTH], output_file[MAX_NAME_LENGTH];



    do {

        printf("\n文件系统操作选项:\n");

        printf("1. 创建文件\n");

        printf("2. 删除文件\n");

        printf("3. 复制文件\n");

        printf("4. 重命名文件\n");

        printf("5. 文件解密\n");

        printf("6. 文件加密\n");

        printf("7. 文件合并\n");

        printf("8. 查看文件内容\n");

        printf("9. 查看当前目录下的文件\n");

        printf("0. 退出\n");

        printf("请选择操作: ");

        scanf(" %c", &choice);



        switch (choice) {

            case '1':

                printf("请输入要创建的文件名: ");

                scanf("%s", filename);

                create_file(filename);

                break;

            case '2':

                printf("请输入要删除的文件名: ");

                scanf("%s", filename);

                delete_file(filename);

                break;

            case '3':

                printf("请输入要复制的文件名: ");

                scanf("%s", filename);

                printf("请输入新的文件名: ");

                scanf("%s", newname);

                copy_file(filename, newname);

                break;

            case '4':

                printf("请输入要重命名的文件名: ");

                scanf("%s", filename);

                printf("请输入新的文件名: ");

                scanf("%s", newname);

                rename_file(filename, newname);

                break;

            case '5':

                printf("请输入要解密的文件名: ");

                scanf("%s", input_file1);

                printf("请输入解密后的文件名: ");

                scanf("%s", output_file);

                decrypt_file(input_file1, output_file);

                break;

            case '6':

                printf("请输入要加密的文件名: ");

                scanf("%s", input_file1);

                printf("请输入加密后的文件名: ");

                scanf("%s", output_file);

                encrypt_file(input_file1, output_file);

                break;

            case '7':

                printf("请输入要合并的第一个文件名: ");

                scanf("%s", input_file1);

                printf("请输入要合并的第二个文件名: ");

                scanf("%s", input_file2);

                printf("请输入合并后的文件名: ");

                scanf("%s", output_file);

                merge_files(input_file1, input_file2, output_file);

                break;

            case '8':

                printf("请输入要查看内容的文件名: ");

                scanf("%s", filename);

                view_file(filename);

                break;

            case '9':

                list_files();

                break;

            case '0':

                printf("退出文件系统\n");

                break;

            default:

                printf("无效的选项,请重新输入\n");

        }

    } while (choice != '0');



    return 0;

}

编译运行:

创建文件 (create_file):

这个函数的主要作用是使用系统调用 open 创建一个新文件。open 函数的第一个参数是要创建的文件名,第二个参数是打开文件的模式,其中包括了文件的读写权限和文件的创建方式(例如,O_WRONLY 表示只写,O_CREAT 表示如果文件不存在则创建,O_TRUNC 表示如果文件存在则将其内容清空)。如果 open 函数调用成功,会返回一个文件描述符,然后我们关闭文件描述符即可。

   

 

删除文件 (delete_file):

这个函数使用系统调用 unlink 来删除指定的文件。unlink 函数的参数是要删除的文件名。如果调用成功,目标文件就会被删除。如果删除失败,函数会返回 -1,并设置相应的错误号。

 

复制文件 (copy_file):

这个函数首先通过 open 函数打开源文件和目标文件,分别获取到源文件和目标文件的文件描述符。然后,利用 read 函数从源文件中读取内容,并通过 write 函数将读取的内容写入到目标文件中。这样就完成了文件的复制操作。

 

重命名文件 (rename_file):

使用 rename 函数来重命名文件。rename 函数的第一个参数是原文件名,第二个参数是新文件名。如果重命名成功,函数返回 0;否则,返回 -1,并设置相应的错误号。

文件解密 (decrypt_file) 和 文件加密 (encrypt_file):

这两个函数的实现基于异或操作的加密算法。异或加密是一种简单的加密方法,通过对文件中的每个字节与一个固定的密钥进行异或运算,来实现加密和解密的功能。在这里,我们选取了一个简单的密钥 XOR_KEY,将文件中的每个字节与这个密钥进行异或运算,从而实现加密和解密的功能。

 

文件合并 (merge_files)

文件合并的实现思路是先打开两个输入文件,然后创建一个新的输出文件。接下来,读取第一个输入文件的内容并将其写入到输出文件中,紧接着再读取第二个输入文件的内容并将其追加到输出文件中。最后,关闭所有文件描述符。

查看文件内容 (view_file)

查看文件内容的实现思路是打开指定的文件,以只读模式读取文件的内容,并将内容逐字节输出到控制台。读取完成后,关闭文件描述符。这样用户就可以看到文件的所有内容。

查看当前目录下的文件 (list_files)

查看当前目录下的文件的实现思路是使用系统调用 opendir 打开当前目录,然后通过 readdir 逐个读取目录中的文件和子目录的名称,并将它们打印到控制台。读取完成后,关闭目录流。这样用户可以看到当前目录中的所有文件和子目录。

5、编程实现一个二级文件系统,要求给出设计分析并列出程序编译、运行过程以及执行结果的部分截屏。(附加题)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <fcntl.h>

#include <dirent.h>

#define MAX_NAME_LENGTH 100

#define MAX_FILES 100



// 文件结构体

typedef struct {

    char name[MAX_NAME_LENGTH];

    char content[1024]; // 假设最大文件大小为 1024 字节

} File;



// 目录结构体

typedef struct {

    char name[MAX_NAME_LENGTH];

    File files[MAX_FILES];

    int num_files;

} Directory;



// 当前目录

Directory current_directory = {"root", {}, 0};



// 创建文件

void create_file(const char *filename) {

    int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

    if (fd == -1) {

        perror("无法创建文件");

        return;

    }

    close(fd);

    printf("文件 %s 已创建\n", filename);

}

// 删除文件

void delete_file(const char *filename) {

    if (unlink(filename) == -1) {

        perror("无法删除文件");

        return;

    }

    printf("文件 %s 已删除\n", filename);

}



// 显示文件列表

void list_files() {

    DIR *d;

    struct dirent *dir;

    d = opendir(".");



    if (d) {

        printf("当前目录下的文件和目录:\n");

        while ((dir = readdir(d)) != NULL) {

            printf("%s\n", dir->d_name);

        }

        closedir(d);

    } else {

        perror("无法打开当前目录");

    }

}



// 主函数

int main() {

    char choice;

    char filename[MAX_NAME_LENGTH];

    char content[1024];



    do {

        printf("\n文件系统操作选项:\n");

        printf("1. 创建文件\n");

        printf("2. 删除文件\n");

        printf("3. 显示文件列表\n");

        printf("4. 退出\n");

        printf("请选择操作: ");

        scanf(" %c", &choice);



        switch (choice) {

            case '1':

                printf("请输入要创建的文件名: ");

                scanf("%s", filename);

                create_file(filename);

                break;

            case '2':

                printf("请输入要删除的文件名: ");

                scanf("%s", filename);

                delete_file(filename);

                break;

            case '3':

                list_files();

                break;

            case '4':

                printf("退出文件系统\n");

                break;

            default:

                printf("无效的选项,请重新输入\n");

        }

    } while (choice != '4');



    return 0;

}

基本思路与上体一样。

三、实验总结和体会(1分)

本次实验中,我学到了以下几点:

文件系统设计与实现:你学会了设计和实现一个简单的文件系统,包括文件和目录的管理、文件的创建、删除和显示等基本功能。

文件操作:你掌握了使用 C 语言中的文件操作相关函数,如 open()、close()、read()、write() 等,以及通过系统调用来实现文件的打开、关闭、读取和写入。

菜单界面设计:你学会了设计简单的菜单界面或命令行界面,使用户能够通过选择不同的操作来完成文件系统的功能。

错误处理:你学会了对用户输入进行验证,并在文件操作过程中处理可能出现的错误,如文件不存在、文件已经存在等情况。

本次实验的难点可能在于文件系统的设计和实现,尤其是需要考虑如何合理地组织文件和目录的结构,以及如何实现各种文件系统功能。另外,对于文件操作函数的理解和运用也可能是一个挑战,特别是对文件的读写操作。然而,通过仔细思考和实践,你可以克服这些难点,并逐步提高对文件系统的理解和掌握。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2089786.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

分享两个方法分析python打包exe

在Python开发中&#xff0c;常常需要将Python脚本打包成独立的可执行文件&#xff0c;以便在没有Python环境的电脑上运行。你是否曾为此感到困惑&#xff0c;不知道该选择哪种工具来实现这一目标&#xff1f;其实&#xff0c;打包Python脚本并不难&#xff0c;关键在于选择合适…

Mybatis框架——缓存(一级缓存,二级缓存)

本章将简单介绍Mybatis框架中的缓存&#xff0c;欢迎大家点赞➕收藏&#xff0c;蟹蟹&#xff01;&#xff01;&#xff01;&#x1f495; &#x1f308;个人主页&#xff1a;404_NOT_FOUND &#x1f308;MyBatis环境搭建步骤&#xff08;超全解析&#xff01;&#xff01;&am…

AI写论文真的可靠吗?免费推荐6款AI论文写作助手

在当今的学术研究和写作领域&#xff0c;AI论文写作助手已经成为不可或缺的工具。这些工具不仅能够提高写作效率&#xff0c;还能帮助研究者生成高质量的论文。以下是六款免费推荐的AI论文写作助手&#xff0c;包括千笔-aipasspaper&#xff0c;它们各自具有独特的功能和优势。…

【hot100篇-python刷题记录】【最小路径和】

R6-多维动态规划篇 好经典的dp题&#xff0c;纯粹的题。 多维动态规划无论是二维还是三维&#xff0c;无非是创建dp表&#xff0c;dp[][][][][][]即可 动态规划式子 dp[i][j]当前值min(dp[i][j-1],dp[i-1][j]) 边界问题处理&#xff1a;是否存在即可。哦对了好像不用这样&a…

探索异步之美:aiohttp库的魔力与奥秘

文章目录 探索异步之美&#xff1a;aiohttp库的魔力与奥秘背景&#xff1a;为何选择aiohttp&#xff1f;什么是aiohttp&#xff1f;如何安装aiohttp&#xff1f;简单函数使用方法场景应用常见Bug及解决方案总结 探索异步之美&#xff1a;aiohttp库的魔力与奥秘 背景&#xff1…

Linux教程七:文件目录类命令ls、cd(图文详解)

默认登录出现一个[用户localhost ~] 代表时登陆用户的家目录 1、 Linux ls命令 基本用法 ls&#xff1a;列出当前目录下的文件和目录&#xff08;不包括以.开头的隐藏文件&#xff09;。ls 目录名&#xff1a;列出指定目录下的文件和目录。 常用选项 -l&#xff1a;以长格式列出…

【Python 报错已解决】`TypeError: ‘method‘ object is not subscriptable`

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 引言 在编程的世界里&#xff0c;我们经常会遇到各种报错&#xff0c;它们像隐藏在代码中的小怪兽&#xff0c;时不时跳出来给…

Java项目:基于SpringBoot+mysql在线拍卖系统(含源码+数据库+答辩PPT+毕业论文)

一、项目简介 本项目是一套基于SSM框架mysql在线拍卖系统 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;eclipse或者idea 确保可以运行&#xff01; 该系统功能完善、界面美观、操作简单、功能齐全、…

常见的性能测试方法!

前言 性能测试划分有很多种&#xff0c;测试方法也有很多种&#xff0c;更确切的说是由于测试方法的不同决定了测试划分的情况&#xff0c;但在测试过程中性能测试的划分没有绝对的界限&#xff0c;常用的有压力测试、负载测试和并发用户测试等。 性能测试的方法主要包括以下…

划分字母区间

划分字母区间 思路&#xff1a; 我觉得这道题最关键的一个思路就是&#xff0c;对于没一个字母&#xff0c;你一定要找到他的最后一次出现的位置&#xff0c;每一个片段必须要比这个大&#xff0c;然后该字符到这个end中其他的字符&#xff0c;也都要找到最后一次出现的位置&…

net6 core 接入nacos 实现服务注册入门使用,心跳检测和负载均衡

net6 core 接入nacos 实现服务注册入门使用&#xff0c;心跳检测和负载均衡 配置中心比较Apollo与Nacos_appollo 和 nacos-CSDN博客 一&#xff1a;安装nacos Release 2.2.3 (May 25th, 2023) alibaba/nacos GitHub 二、配置Nacos 注*Nacos 是使用的mysql 数据库&#x…

谷粒商城实战笔记-282~283-商城业务-订单服务-提交订单的问题

文章目录 一&#xff0c;282-商城业务-订单服务-提交订单的问题调试过程中出现的问题services面板介绍什么是 Services Panel&#xff1f;主要作用解决的痛点使用方法 二&#xff0c;283-商城业务-分布式事务-本地事务在分布式下的问题分布式事务问题解决方案分布式事务处理流程…

Hubspot AI 工具| 使用 6 款 HubSpot AI 工具,提升初创团队海外营销与销售效率

看看 HubSpot AI 工具如何完美助力中国出海与外贸企业加速落地全球 GTM 策略吧~ 在日益竞争激烈的全球市场中&#xff0c;初创企业想要获得成功&#xff0c;必须有效将产品推向市场&#xff0c;并建立客户基础&#xff0c;与竞争对手一较高下。 这需要精心策划的进入市场&…

常用于单北斗多频定位导航模块资料:ATGM332D-F8N

单北斗多频定位导航模块的高精度定位优点描述&#xff1a; 1、多频信号融合&#xff1a;单北斗多频定位导航模块能够接收和处理来自北斗卫星系统的多个频段信号。通过多频信号的融合处理&#xff0c;可以显著提高定位的精度。因为不同频段的信号在传播过程中受到的影响不同&…

力扣134-加油站(java题解)

题目链接&#xff1a;134. 加油站 - 力扣&#xff08;LeetCode&#xff09; 前情提要&#xff1a; 因为本人最近都来刷贪心类的题目所以该题就默认用贪心方法来做。 贪心方法&#xff1a;局部最优推出全局最优。 如果一个题你觉得可以用局部最优推出全局最优&#xff0c;并…

IO进程day06(进程间通信、信号、共享内存)

目录 【1】进程间通信 IPC 1》 进程间通信方式 2》 无名管道 1> 特点 2> 函数接口 3> 注意事项 练习&#xff1a;父子进程实现通信&#xff0c;父进程循环从终端输入数据&#xff0c;子进程循环打印数据&#xff0c;当输入quit结束。 3》有名管道 1> 特点 …

24数学建模国赛准备!!!!(10——马氏链模型)

详细获取资料方式在文章末尾&#xff01;&#xff01;&#xff01;&#xff01; 点击链接加入群聊获取资料以及国赛助力https://qm.qq.com/q/NGl6WD0Bky &#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&…

多场景建模: STAR(Star Topology Adaptive Recommender)

之前&#xff0c;分享了一篇关于多任务学习的文章&#xff1a;多任务学习MTL模型&#xff1a;MMoE、PLE&#xff0c;同样的还有关于多任务学习中的多目标loss优化策略。 这篇文章则开始一个与多任务学习有着紧密联系的系列&#xff1a;多场景建模学习。 前言 首先&#xff0…

[Raspberry Pi]如何利用docker執行motioneye,並利用Line Notify取得即時通知和照片?

[Motioneye]How to setup motion detection and send message/image for Line Notify 無意間&#xff0c;翻了一本關於樹莓派的書籍&#xff0c;除了樹莓派的簡介和應用外&#xff0c;也包含初階和高階的Linux運作邏輯&#xff0c;書籍結構相當完整&#xff0c;也因此需要花時間…

DAC: High-Fidelity Audio Compression with Improved RVQGAN

Rithesh KumarDescript, Inc.2023NIPS code 文章目录 abstratmethod abstrat 44.1k音频编码为8k bps&#xff0c;接近90x压缩比&#xff1b;可以处理speech, musiccodebook collapse: 部分码本没有利用到。----quantizer dropout &#xff1a;允许单个模型支持可变比特率&…