Linux文件管理模拟实验

一、使用标准C库实现文件复制程序

1、数据准备

新建出testcp目录,然后在testcp目录中新建出src.txt

$ mkdir ~/testcp
$ cd ~/testcp
$ nano src.txt

内容如下:

a b c
d e f
1 2 3

2、编写C程序 my_copy.c

$ nano my_copy.c

内容如下:

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

#define SRC_PATH "/home/hadoop/testcp/src.txt"
#define TRG_PATH "/home/hadoop/testcp/target.txt"

#define BUF_SIZE 1024

int main(void){
    FILE *fpSrc, *fpTrg;

    int c = 0;
    char buf[BUF_SIZE] = {0};

    if((fpSrc = fopen(SRC_PATH, "rb")) == NULL){
        printf("get src file failed\n");
        return -1;
    }

    if((fpTrg = fopen(TRG_PATH, "wb")) == NULL){
        printf("get target file failed\n");
        return -1;
    }

    while((c = fread(buf, sizeof(char), BUF_SIZE, fpSrc)) > 0){
        fwrite(buf, sizeof(char), c, fpTrg);
    }

    if(fclose(fpSrc) != 0)
        puts("close src file failed!\n");

    if(fclose(fpTrg) != 0)
        puts("close target file failed!\n");

    return 0;
}

 

3、编译C程序

$ gcc my_copy.c

编译成功后,ls查看编译生成了a.out文件

$ ls
a.out  my_copy.c  src.txt

4、执行a.out文件

$ ./a.out

查看结果生成了target.txt文件

$ ls
a.out  my_copy.c  src.txt  target.txt

验证target.txt的内容是否与src.txt的内容一致,一致则为正确复制

$ cat target.txt

 

二、使用Linux系统函数实现文件复制程序

1、数据准备

新建testcp2目录,并在testcp2目录下新建src.dat文件

$ mkdir ~/testcp2
$ cd ~/testcp2
$ nano src.dat

src.dat文件内容如下:

a b c
d e f
123
456

2、编写C程序 my_copy2.c

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define SRC_PATH "/home/hadoop/testcp2/src.dat"
#define TRG_PATH "/home/hadoop/testcp2/target.dat"

#define BUF_SIZE 1024

int main() {

    char buf[BUF_SIZE] = {0};

    int fdSrc, fdTrg;
    int len;

    fdSrc = open(SRC_PATH, O_RDWR | O_CREAT);
    fdTrg = open(TRG_PATH, O_RDWR | O_CREAT);

    while(len = read(fdSrc, buf, BUF_SIZE)){
        write(fdTrg, buf, len);
    }

    if(close(fdSrc) != 0)
        printf("close src file failed!\n");

    if(close(fdTrg) != 0)
        printf("colse target file failed!\n");

    return 0;
}

3、编译

$ gcc my_copy2.c

4、执行

$ ./a.out

5、查看结果

$ ls
a.out  my_copy2.c  src.dat  target.dat

$ ll
total 32
drwxrwxr-x  2 hadoop hadoop 4096 Mar  9 17:28 ./
drwxr-xr-x 10 hadoop hadoop 4096 Mar  9 17:22 ../
-rwxrwxr-x  1 hadoop hadoop 8872 Mar  9 17:28 a.out*
-rw-rw-r--  1 hadoop hadoop  706 Mar  9 17:28 my_copy2.c
-rw-rw-r--  1 hadoop hadoop   20 Mar  9 17:26 src.dat
--wxrwS--T  1 hadoop hadoop   20 Mar  9 17:28 target.dat*

hadoop@wangchi:~/testcp2$ sudo cat target.dat
[sudo] password for hadoop:
a b c
d e f
123
456

 注意:复制之后src.dat与target.dat的权限不一致。

在执行一次

$ ll
-rw-rw-r--  1 hadoop hadoop   20 Mar  9 17:26 src.dat
-rwxr-----  1 hadoop hadoop   20 Mar  9 17:43 target.dat*

在执行一次
-rw-rw-r--  1 hadoop hadoop   20 Mar  9 17:26 src.dat
--w-------  1 hadoop hadoop   20 Mar  9 17:45 target.dat

发现每次执行权限都有变化(没有规律),但还是不一致。

经过验证,需要提前创建出target.dat文件,最后的权限才一致。

 

三、优化已实现的文件复制程序(优化题目一)

my_copy_update.c

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


#define BUF_SIZE 1024

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

    char const *src_path = argv[1];
    char const *trg_path = argv[2];

    FILE *fpSrc, *fpTrg;

    int c = 0;
    char buf[BUF_SIZE] = {0};

    if((fpSrc = fopen(src_path, "r+")) == NULL){
        printf("get src file failed\n");
        return -1;
    }

    if((fpTrg = fopen(trg_path, "w+")) == NULL){
        printf("get target file failed\n");
        return -1;
    }

    while((c = fread(buf,sizeof(char),BUF_SIZE,fpSrc)) > 0){
        fwrite(buf, sizeof(char), c, fpTrg);
    }

    if(fclose(fpSrc) != 0)
        puts("close src file failed!\n");

    if(fclose(fpTrg) != 0)
        puts("close target file failed!\n");

    return 0;
}

 

完成! enjoy it!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
【实验目的】 1. 了解文件系统的原理; 2. 用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程。从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。 【实验准备】 1.文件的逻辑结构  顺序文件  索引文件  索引顺序文件  直接文件和哈希文件 2.外存分配方式  连续分配  链接分配  索引分配 【实验内容】 1. 实验要求 要求设计一个 n个用户的文件系统,每次用户可保存m个文件,用户在一次运行中只能打开一个文件,对文件必须设置保护措施,且至少有Create、delete、open、close、read、write等命令。 2. 实验题目  设计一个10个用户的文件系统,每次用户可保存10个文件,一次运行用户可以打开5个文件。  程序采用二级文件目录(即设置主目录[MFD])和用户文件目录(UED)。另外,为打开文件设置了运行文件目录(AFD)。  为了便于实现,对文件的读写作了简化,在执行读写命令时,只需改读写指针,并不进行实际的读写操作。 因系统小,文件目录的检索使用了简单的线性搜索。文件保护简单使用了三位保护码:允许读写执行、对应位为 1,对应位为0,则表示不允许读写、执行。程序中使用的主要设计结构如下:主文件目录和用户文件目录( MFD、UFD)打开文件目录( AFD)(即运行文件目录)。 M D F 用户名 文件目录指针 用户名 文件目录指针 U F D 文件名 保护码 文件长度 文件名 A F D 打开文件名 打开保护码 读写指针

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值