实验四:文件备份实验

一、实验目的

1.熟悉 Linux 文件系统的文件和目录结构
2.掌握文件系统的基本特征
3.掌握常用文件操作函数。

二、实验内容

编写 C 程序模拟实现 Linux 文件系统的简单 I/O 流操作:备份文件,将源文件 source.dat 备份为 target.dat 文件。要求:
1)使用 C 库函数实现文件备份;
2)使用系统调用函数实现文件备份

三、实验步骤

使用C库函数实现文件备份

#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);
        }
}

使用Linux系统调用文件函数

#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);
        }
}

四、实验结果

C库函数实现结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Linux系统调用实现

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

五、实验思考

1.使用系统调用函数 open(),read(),write(),close()实现简单文件备份的原理是什么?

系统调用函数open(),read(),write(),close()是通过文件描述符来实现文件的备份,文件描述符就是一些数值,用来描述文件的配置情况,通过文件描述符的管理,open建立了一条到文件或设备的访问路径,如果调用成功,返回一个可以被read、write等其他系统调用的函数使用的文件描述符。write的作用是把缓冲区buf的前nbytes个字节写入到文件描述符关联的文件中,返回实际写入的字节数。read系统调用的作用是从与文件描述符相关的文件里读入nbytes个字节的数据,并把它们放到数据区buf中,返回读入的字节数。close函数的作用是关闭文件描述符和其对应的文件之间的关联。

2.使用 C 库函数 fopen(), fread(), fwrite(), fclose() 来实现简单文件备份的原理是什么?

在linux系统中,文件和设备都被看做事数据流,进行操作之前,必须先将流打开, 可以通过调用库函数fopen()打开一个流,库函数fopen()的返回值为一个FILE结 构指针,此结构中包含对所打开的流进行操作所需的全部信息。当流操作完成后, 需要执行清空缓冲区,保存数据等操作,所以这个时候需要将流关闭,调用函数 fclose()来完成。在实现文件备份的过程中,实际上是建立了两个流,把其中一个 流中的数据写入另一个流中从而实现备份。

3.上述两者的区别是什么?

在这里插入图片描述

系统调用open()等函数和C库函数fopen()等函数都是对文件的操作的相关函数, 但前者是在内核模式中运行的,且是在文件描述符的基础上进行,而后者则是在用 户模式下运行的,且是在数据流的基础上进行,而每一个文件流都对应一个底层的 文件描述符。在进行C库函数fopen()等函数的调用时,相应的操作会在进入内核 中以相应系统调用函数实现。

  • 13
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值