📄 fat.txt
字号:
{
read(filedes,&newFat,sizeof(FAT));//读取fat
if(newFat.nextBlock==-2)
break;
}
//若没有找到空闲块,则结束写入
//************
if(newFatPos==BLOCK_NYM)
break;
//将当前块的fat值改为新块的块号
//************
fat.nextBlock=newFatPos;
//将新块的fat值改为-1,表明这是文件的最后一块
//************
newFat nextBlock=-1;
lseek(filedes,volumeInfo.fatAreaAddress+newFatPos*sizeof(FAT),SEEK_SET);
write(filedes,&newFat,sizeof(FAT));
//当前块号改为新块的块号
//************
block=newFatPos;
}
else
{
//将当前块号,改为下一块的块号
//************
block=fat.nextBlock;
}
//块内偏移blockPos,设置到块首
//************
blockPos=0;
}
//在当前块的块内偏移位置写入数据
//************
lseek(filedes,volumeInfo.dataAreaAddress+block*BLOCK_SIZE+blockPos,SEEK_SET);
write(filedes,buf+finishNum);
blockPos++;
finishNum++;
}
//修改文件描述信息中的文件的大小
//************
if(finshNum+writePos>fcb.size)
{
fcb.size=finishNum+writePos;
lseek(filedes,volumeInfo.fcbAreaAddress+fcbPos*sizeof(FCB),SEEK_SET);
write(filedes,&fcb,sizeof(FCB));
}//返回实际写入的数据量
return finishNum;
}
//读文件。fileName是要执行操作的文件名,buf是存放读出的数据的数组
//readNum是要读取的数据量,readPos是读取位置
//返回值是实际读出的数据量
long readFile (char* fileName,char* buf,long readNum,long readPos)
{
FCB fcb;//文件描述信息
long block;//当前块号
long blockPos;//块内偏移
long finishNum;
FAT fat;
long fcbPos =openFile(fileName,&fcb);
if(readPos>fcb.size)
{
readPos=fcb.size;
}
block=fcb.headBlock;
blockPos=readPos%BLOCK_SIZE;
finishNUM=0;
long moveTime;// block要移动的次数
//计算block要移动的次数moveTime
//例如:若writePos=510,则要将block要移动到该文件的第1个盘块,需移动0次, moveTime=0
//若writePos=600,则要将block移动到该文件的第2个盘块,需移动1次, moveTime=1
//************
moveTime=writePos/BLOCK_SIZE;
//移动block
while(moveTime >0)
{
//读取当前块的fat值,获取下一块的块号
//************
lseek(filedes,volumeInfo.fatAreaAddress+block*sizeof(fat),SEEK_SET);
read(filedes,&fat,sizeof(FAT));
//将block移动到下一块号
//************
block=fat.nextBlock;
moveTime --;
}
while(finishNum<readNum)
{
lseek(filedes,volumeInfo.dataAreaAddress+block*BLOCK_SIZE+blockPos,SEEK_SET);
read(filedes,&buf,sizeof(FAT))
blockPos++;
finishNum++;
}
return readNum;
}
//******************************************************************************
//以下我的框架部分 主要是把返回值定为long 如果不成功则返回-1
//创建文件夹. meunName是要创建的文件夹名
long mkdir(char *meunName)
{
//***********
}
long rmdir(char *meunName)
{
//*************
}
void cd()
{
//***************
}
long inFile(char *fileName)
{
//*******************
}
long outFile(char *fileName)
{
//***************
}
long remeunName(char *meunName,char *newmeunName)
{
//************
}
long refileName(char *fileName,char *newfileName)
{
FCB fcb; //文件描述信息
long block; //当前块号
long blockPos; //块内偏移
long finishNum; //记录已经写入的数据量
FAT fat;
long fcbPos =openFile(fileName,&fcb);
//*************
}
long lsFile(char *fileName)
{
//*******************
}
void clear()
{
system("clear");
}
void cd_back()
{
//***********************
}
//主函数
int main()
{
cout<<"******Welcome to Virtual File System V1******"<<endl;//输出欢迎信息
version();打印信息
filedes=open("mydisk",O_RDWR|O_CREAT); //打开模拟文件系统的文件
//检查存储介质是否已经被格式化
lseek(filedes,0,SEEK_SET);//将读写指针移动到盘卷总信息处
//读取盘卷总信息
if(read(filedes,&volumeInfo,sizeof(VolumeInfo))<sizeof(VolumeInfo))
{
cout<<"the Virtual File System is unformated!"<<endl;
cout<<"格式化将删除磁盘上所有资料,是否继续?<Y/N>:"
if (getchar()!='y'&&getchar()!='Y')
return 0;
else
format();
}
else if(volumeInfo.fcbAreaAddress!=sizeof(VolumeInfo) ||
volumeInfo.fatAreaAddress!=volumeInfo.fcbAreaAddress+sizeof(FCB)*FCB_NUM ||
volumeInfo.dataAreaAddress!=volumeInfo.fatAreaAddress+sizeof(FAT)*BLOCK_NUM)
{
cout<<"the Virtual File System is unformated!"<<endl;
format();
}
//循环处理用户输入的操作命令
while(true)
{
getCommand();//显示命令提示符,并接收用户输入的命令
//处理用户命令
if(strcmp(command[0],"exit")==0)
{
return 0;//退出程序
}
else if(strcmp(command[0],"help")==0)
{
showHelp();//显示帮助
}
else if(strcmp(command[0],"format")==0)
{
format();//格式化虚拟文件系统
}
else if(strcmp(command[0],"showVolumeInfo")==0)
{
showVolumeInfo();//显示盘卷总信息
}
else if(strcmp(command[0],"ls")==0)
{
ls();//显示目录内容
}
else if(strcmp(command[0],"createFile")==0)
{
if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
createFile (command[1]);//建立文件
}
else if(strcmp(command[0],"deleteFile")==0)
{
if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
deleteFile (command[1]);//删除文件
}
else if(strcmp(command[0],"writeFile")==0)
{
if(commandNum<3)
{
cout<<"this command must take two argument!"<<endl;
continue;
}
writeFile (command[1],command[2],sizeof(command[2]),0);//写入文件
}
else if(strcmp(command[0],"readFile")==0)
{
if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
char buf[100];
readFile (command[1],buf,100,0);//读文件
cout<<buf<<endl;//输出文件内容
}
else if(strcmp(command[0],"clear")==0)
{
clear(); //清屏
}
else if(strcmp(command[0],"mkdir")==0)
{
if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
mkdir(command[1]);
}
else if(strcmp(command[0],"rm")==0)
{
if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
rm(command[1]);
}
else if(strcmp(command[0],"outFile")==0)
{
if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
outFile(command[1]);
}
else if(strcmp(command[0],"inFile")==0)
{ if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
inFile(command[1]);
}
else if(strcmp(command[0],"cd..")==0)
{
cd_back();
}
else if(strcmp(command[0],"cd")==0)
{ if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
cd(command[1]);
}
else if(strcmp(command[0],"remeunName")==0)
{ if(commandNum<3)
{
cout<<"this command must take two argument!"<<endl;
continue;
}
remeunName(command[1],command[2]);
}
else if(strcmp(command[0],"refileName")==0)
{ if(commandNum<3)
{
cout<<"this command must take two argument!"<<endl;
continue;
}
refileName(command[1],command[2]);
}
else if(strcmp(command[0],"lsFile")==0)
{ if(commandNum<2)
{
cout<<"this command must take a argument!"<<endl;
continue;
}
lsFile(command[1]);
}
else
{
cout<<"Command Not Found!"<<endl;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -