文件系统,很水。。。。。不过还是贴一下

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define FNUM 10//这个文件系统实在是非常简单。。。之前自己也在网上找了一个文件系统的代码,很长,地方看不懂,就没有模仿那个写。。。

#define FOPEN 5//一个用户对应一根文件

#define USER 10//oper=1结束,oper=2创建,oper=3删除,oper=4打开,oper=5关闭

int coun[10]={0};

int ope[10]={0};

int oper=0;

typedef struct file{

    int num;

    int read;

    int write;

    int exe;

    int data;

    int length;

}file;//保存文件的属性和内容

struct mfd{

    int name;

    struct file fil;

}mfd[USER];//用户目录

struct afd{

    int numf;

    int protect;

    int rea;

    int writ;

}afd[USER];//执行时候所用的结构体

void inituser(){

    int i;

    for(i=0;i<USER;i++){

        mfd[i].name=i;

    }

}//初始化十个用户

void initafd(){

    int i;

    for(i=0;i<10;i++){

        afd[i].numf=0;

        afd[i].protect=0;

        afd[i].rea=0;

        afd[i].writ=0;

    }

}//将执行结构体中的量都置为0

void create(int n,file f){

    if(coun[n]<=10){//每个用户可以创建十次文件

        printf("please input the name of file ");

        scanf("%d",&f.num);

        printf("please input the length of file ");

        scanf("%d",&f.length);

        printf("please input the data of file ");

        scanf("%d",&f.data);

        printf("please input the control number of writing ");

        scanf("%d",&f.write);

        printf("please input the control number of reading ");

        scanf("%d",&f.read);

        printf("please input the control number of exe ");

        scanf("%d",&f.exe);

        coun[n]++;

        mfd[n].fil=f;//将创建好的文件放到相应的用户目录下

    }

    else{

        printf("%d can not create file!\n",n);

    }

}

void delef(int n){

    printf("%d user is deleting file %d\n",n,mfd[n].fil.num);

    mfd[n].fil.num=0;

    mfd[n].fil.data=0;

    mfd[n].fil.exe=0;

    mfd[n].fil.length=0;

    mfd[n].fil.read=0;

    mfd[n].fil.write=0;

    printf("delete over!\n");

}//将相应用户目录中的值都置为0,表示文件已经删除

void open(int n){

    if(ope[n]<=5){

        int i;

        //将执行所用的量放入相应的执行结构体中

        printf("please input the number of file :");

        scanf("%d",&afd[n].numf);

        printf("please input the protect number of file :");

        scanf("%d",&afd[n].protect);

        printf("please input the writing number of file :");

        scanf("%d",&afd[n].writ);

        printf("please input the reading number of file :");

        scanf("%d",&afd[n].rea);

        for(i=0;i<10;i++){

            if(mfd[n].fil.num==afd[n].numf){

                if((mfd[n].fil.read==1)&&(afd[n].rea==1)){//文件可读并且用户申请读文件时

                    //这里也可以将文件中data打印出来

                    printf("%d is reading file %d\n",n,afd[n].numf);

                }

                else if((mfd[n].fil.write==1)&&(afd[n].writ==1)){//文件可写,用户也要求写

                    printf("%d is writing file %d\n",n,afd[n].numf);

                }

                else {//执行文件

                    printf("%d is running file %d\n",n,afd[n].numf);

                }

                break;

            }

        }

    }

}

void close(int n){//关闭文件就是将相应的文件执行结构体值设为0

    printf("%d user is closing file %d\n",n,afd[n].numf);

    afd[n].numf=0;

    afd[n].protect=0;

    afd[n].rea=0;

    afd[n].writ=0;

    printf("file has closed!\n");

}

int main(){

    int u;

    int i=0;

    //mfd[USER]=(struct mfd*)malloc(10*(sizeof(struct mfd)));

    printf("welcome to my file system!\n");

    inituser();

    initafd();

    while(i<20){

        printf("please input the user number:");

        scanf("%d",&u);

        printf("please input what do you want to do with file:");

        scanf("%d",&oper);

        if(oper==1){

            printf("the operation is over!\n");

            break;

        }

        else if(oper==2){

            struct file f;

            create(u,f);//

        }

        else if(oper==3){

            delef(u);

        }

        else if(oper==4){

            open(u);

        }

        else if(oper==5){

            close(u);

        }

        i++;

    }

    printf("welcome for the next time!\n");

    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值