操作系统实验四

文件管理

hello 我是橘子,这个是我最满意的一个吧,因为真正实现了在内存中建立文件夹和删除文件夹,也是花费时间最长的一个文件
文件管理
●基本要求:利用磁盘文件实现操作系统的文件管理功能,主要包括目录结构的管理、外存空间的分配与释放以及空闲空间管理三部分。
●参考学时:9学时
●实验提示:
1、通过初始化操作建立一个模拟外存空间的虚拟磁盘文件,在该文件中保存目录和文件内容。创建该文件时应创建初始的根目录内容、文件分配表。根目录实为一特殊文件,其开始内容为空,大小为一个块。
2、文件目录项(可以采用FCB格式)应包括类型(目录 or文件)、创建日期、大小、第一个磁盘块块号。
3、显示命令提示符“$”,并根据输入命令完成相应的文件操作:
●MD(创建子目录):创建目录文件,并在父目录文件中增加目录项。
●CD(切换工作目录):根据当前目录切换到指定目录。
●RD(删除子目录):搜索所要删除的目录是否为空目录,若是则删除。
●MK(创建空文件):创建指定大小的文件(如输入命令 “mk test 2000”,表示创建大小为2000字节的test文件),并在父目录中添加文件名称;还应对FAT表进行适当修改。
●DEL(删除文件):如果所要删除的文件存在,则删除,同时修改父目录内容;还应对FAT表进行适当修改。
●DIR:列出当前目录的所有目录项。
●FORMAT:根据进一步的虚拟磁盘文件名和块个数信息创建出虚拟磁盘文件。
●*TREE:根据磁盘文件和目录按着树形结构加以显示。
4、实现提要
●首先应确定采用FAT16表,块大小固定为1024Byte;
●定义目录项结构:
struct FCB{ //文件或目录控制块
char name[8]; //文件名最长不超过8个字符,这样整个FCB大小正好为32Byte
int size; //大小
int first_block; //第一块块号
char type; //类型,1为文件,2为目录,0为已删除目录项
char datetime[15]; //日期时间,格式为yyyymmdd hhmmss
};
●定义两个宏:EMPTY_BLOCK和LAST_BLOCK,用于表示FAT表中的特殊内容
#define EMPTY_BLOCK 0x0000
#define LAST_BLOCK 0xFFFF

●评分标准(满分15分):
要求必须完成以下几个方面的内容(10-12分):
能够在磁盘文件基础上模拟外存分配与回收流程;
●支持dir、md、cd、rd命令。
也可实现如下扩充要求(3-5分):
●可较方便查看位示图、索引节点、目录树关系
●能够实现创建文件的mknod命令以及删除文件的del命令,可对所创建的文件内容进行简单的编辑。

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<string>
#include<time.h>
#include<direct.h>
#include<windows.h>
using namespace std;

struct FCB
{
	char name[10];//文件名最长不超过8个字符,这样整个FCB大小正好为32Byte
	int size1;//大小
	int fat[64];//块号
	double bit;
	int first;//fat的角标
	int first_block;//第一块块号
	int type;//类型,1为文件,2为目录,0为已删除目录项
	char time[20];	//日期时间,格式为yyyymmdd hhmmss
	char path[100];
    char parent[100];
};

struct FCB fcb[50];

int f=0;
char temp[100]="D:/";//记录当前位置
int arry[8][8];
int step=0;//树的深度
char steps[100][100];//保存路径

void bitmap()//创建的位图
{

    int i,j;
    for(i=0;i<8;i++)
    for(j=0;j<8;j++)
		arry[i][j]=rand()%2-0;
    for(i=0;i<8;i++)
    {
		for(j=0;j<8;j++)
			cout<<arry[i][j]<<" ";
		cout<<'\n';
    }
}
void outmap()//输出位图
{
    int i,j;
    for(i=0;i<8;i++)
    {
		for(j=0;j<8;j++)
			cout<<arry[i][j]<<" ";
		cout<<'\n';
    }
}
string getCurrentTimeStr()//得到时间
{
	time_t t = time(NULL);
	char ch[64] = {0};
	strftime(ch, sizeof(ch) - 1, "%Y-%m-%d %H:%M:%S", localtime(&t));
	return ch;
}
void md()//创建目录
{
	cout<<temp;
	int i;
    string name;
    //cout<<"请输入名字:";
    cin>>name;

    string nn;
	nn=temp;
    nn+=name+"/";
    char aa[20];
    strcpy(aa,nn.c_str());
	strcpy(fcb[f].path,nn.c_str());
    mkdir(aa);

    strcpy(fcb[f].name,name.c_str());
    fcb[f].type=2;
    string tim=getCurrentTimeStr();
    strcpy(fcb[f].time,tim.c_str());
	strcpy(fcb[f].parent,temp);
	cout<<"创建成功"<<endl;
	f++;
}
void cd()
{
   
     cout<<temp;
    string name,nn;
    cout<<"cd:";
    cin>>name;
	nn=temp+name+"/";
    char aa[10];
    strcpy(aa,nn.c_str());
    int i,j;
	if(f==0)
	cout<<"当前没有目录可切换";
    for(i=0;i<f;i++)
	{
		if(strcmp(fcb[i].path,aa)==0)
		{

			  strcpy(temp,fcb[i].path);
			  cout<<"当前在"<<temp<<"目录下";
			  break;
		}
		if(i==f)
		{
			cout<<"没有找到此文件夹";
		}
	}
	cout<<endl;
}
void back()
 {
      strcpy(temp,"D:/");
	  cout<<"当前在"<<temp<<"目录下"<<endl;
 }
 void rd()//删除目录
 {
	cout<<temp<<" ";
    cout<<"rd:";
    string name,nn;
    cin>>name;
	nn=temp+name+"/";
    int i,j,k,x,y,m;
    char aa[100];
    strcpy(aa,nn.c_str());
    for(i=0;i<f;i++)
	{
		if(strcmp(fcb[i].path,aa)==0)
		{
			for(j=0;j<f;j++)
			{
				if(strcmp(fcb[j].parent,fcb[i].path)==0)
				{
				cout<<"此文件不能删除"<<endl;
				break;
				}
			}
			if(j==f)
			{
			  rmdir(aa);
			  cout<<"删除成功"<<endl;
			}  
			break;
		}
	}
	if(i==f)
	{
		cout<<"当前在"<<temp<<"目录下"<<endl;
		cout<<"没有此文件"<<endl;
		return;
	}
	for(k=i;k<f;k++)
		fcb[k]=fcb[k+1];
	f--;
}
 void mk()//创建文件
 {
    cout<<temp;
	cout<<"mk :";
	FILE *fp;
    string name,nn;
    int size1;
	char bb[100],aa[30];
    cin>>name;
	cout<<"请输入大小:";
    cin>>size1;
	cout<<"请输入文件的内容:"<<endl;
	cin>>bb;
	nn=temp;
    nn+=name;
	strcpy(aa,nn.c_str());
	strcpy(fcb[f].path,nn.c_str());
	fp=fopen(aa,"w");
    fprintf(fp,bb);
    fclose(fp);

    strcpy(fcb[f].name,name.c_str());
    fcb[f].size1=size1;
    fcb[f].type=1;
    string tim=getCurrentTimeStr();
    strcpy(fcb[f].time,tim.c_str());
	strcpy(fcb[f].parent,temp);
    int i,j,m=0;
    double temp1=0;
    temp1 = size1/1024.0;
    fcb[f].bit=temp1;

    for(i=0;i<8;i++)
		for(j=0;j<8;j++)
			if((arry[i][j]==0)&&( fcb[f].first<temp1))
			{
                arry[i][j]=1;
                fcb[f].fat[fcb[f].first]=i*8+j;
                fcb[f].first++;

			}
	fcb[f].first_block=fcb[f].fat[0];
	for(i=0;i<temp1;i++)//输出fat表位置
		cout<<i<<"   "<<fcb[f].fat[i]<<endl;
	f++;
 }
 void del()// 删除文件
 {  
	 cout<<temp<<" ";
     cout<<"del:";
     string name,nn;
     cin>>name;
	 nn=temp+name;//路径
     int i,j,k,x,y,m;
     char aa[100];
     strcpy(aa,nn.c_str());
     for(i=0;i<f;i++)
	 {
		if(strcmp(fcb[i].path,aa)==0)
		{
			for(j=0;j<f;j++)
			{
				if(strcmp(fcb[j].parent,fcb[i].path)==0)
				{
				cout<<"此文件不能删除"<<endl;
				break;
				}
			}
			if(j==f)
			{
				for(m=0;m<fcb[i].bit;m++)
				{
				  x=(fcb[i].fat[m])/8;
                  y=(fcb[i].fat[m])%8;
                  arry[x][y]=0;
			    }
			  remove(aa);
			  cout<<"删除成功"<<endl;
			}  
			break;
		}
	}
	if(i==f)
	{
		cout<<"当前在"<<temp<<"目录下"<<endl;
		cout<<"没有此文件"<<endl;
		return;
	}
    for(k=i;k<f;k++)
				  fcb[k]=fcb[k+1];
    f--;
           

 }
 void dir()// 显示当前目录下的文件
 {
     int i,j,k,l;
     if(strcmp(temp,"D:/")==0)
     {
         cout<<"当前在根目录下"<<endl;
         for(j=0;j<f;j++)
			 if(strcmp(fcb[j].parent,"D:/")==0)
			 {  
				 cout<<"名字"<<"\t"<<"大小"<<"\t"<<"类型"<<"\t"<<"创建时间"<<"\t\t"<<"父亲"<<endl;
                 cout<<fcb[j].name<<"\t"<<fcb[j].size1<<"\t"<<fcb[j].type<<"\t"<<fcb[j].time<<"------ "<<fcb[j].parent<<" ";
                 for(k=0;k<fcb[j].bit;k++)
					 cout<<fcb[j].fat[k]<<" ";
				 cout<<endl;
			 }
     }
     else
     {   
		 cout<<"当前在"<<temp<<"目录下"<<endl;
         for(i=0;i<f;i++)
			 if(fcb[i].parent==temp)
			 {
				 cout<<"名字"<<"\t"<<"大小"<<"\t"<<"类型"<<"\t"<<"创建时间"<<"\t\t"<<"父亲"<<endl;
				 cout<<fcb[j].name<<"\t"<<fcb[j].size1<<"\t"<<fcb[j].type<<"\t"<<fcb[j].time<<"------ "<<fcb[j].parent<<" ";
                 for(l=0;l<fcb[i].bit;l++)
					 cout<<fcb[i].fat[l]<<" ";
				 cout<<endl;
			 }
     }
 }
void save()
{
    ofstream w;
    w.open("D:\\3.txt");
    for(int i=0;i<f;i++)
		w.write((const char*)(&fcb[i]),sizeof(fcb[i]));
    w.close();

    ofstream w1;
    w1.open("D:\\4.txt");
    w1.write((const char*)(&f),sizeof(f));
    w1.close();
}
void read()
{
    int qq;
    ifstream r1;
    r1.open("D:\\4.txt");
    r1.read((char*)(&qq),sizeof(qq));
    cout<<qq<<endl;
    f=qq;
    r1.close();

    ifstream r;
    r.open("D:\\3.txt");

    int i;
    for(i=0;i<f;i++)
    {
       r.read((char*)(&fcb[i]),sizeof(fcb[i]));
       cout<<fcb[i].name<<endl;
    }
    r.close();

}
void tree()
{
   int n=0;
   int i,j;
  // if(n==0)
  // cout<<fcb[0].name<<endl;
   for(i=0;i<f;i++)
   {
	   if((strcmp(fcb[i].parent,"D:/"))==0)
	   {
		    cout<<fcb[i].name<<endl;
	   }
	   for(j=0;j<f;j++)
	   {
		 if(strcmp(fcb[j].parent,fcb[i].path)==0)
		 {
			 cout<<"\t-->"<<fcb[j].name<<endl;
		 }
	   }
   
   }
   cout<<endl;
}

int main()
{
	 int i;
	 for(i=0;i<50;i++)
	{
		fcb[i].first=0;
	}
	 bitmap();
	 int n=1;
	  while(n)
    {
		//cout<<"当前在盘下"<<temp<<endl;
        cout<<"1-------------MD(创建目录)\n";
		cout<<"2-------------CD(切换目录)\n";
		cout<<"3-------------回到根目录\n";
	    cout<<"4-------------RD(删除目录)\n";
		cout<<"5-------------MK(创建文件)\n";
		cout<<"6-------------DEL(删除文件)\n";
		cout<<"7-------------DIR(显示当前目录下的文件)\n";
		cout<<"8------------保存\n";
		cout<<"9------------读出\n";
		cout<<"10------------TREE\n";
		cout<<"11------------显示位示图\n";
		cin>>i;
		switch(i)
		{
			case 1:md();break;

			case 2:cd();break;

			case 3:back();break;
			
			case 4:rd();break;
			
			case 5:mk();break;
			
			case 6:del();break;

			case 7:dir();break;
				
			case 8:save();break;
			
			case 9:read();break;
			
			case 10:tree();break;

		    case 11:outmap();break;

			case 0:exit(0);break;

		}
	  
	 }
	    return 0;

猜你喜欢

转载自blog.csdn.net/qq_40172319/article/details/88873379
今日推荐