操作系统实训_基于多级目录的文件管理系统 2022-07-07 2.3k- 11m- 实训-实训fxy 1、实训报告:github实训报告 2、代码: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358//基于多级文件目录的文件管理系统#include<bits/stdc++.h>#include <windows.h>#include <stdio.h>#include <ctime>#include <cstdio>#define endl '\n'using namespace std;const int N = 1e4 + 10;const int M = 110;struct file{ string name; vector<string> vet; //当前文件中存储的内容 string where;//用来记录当前文件的位置 int year,month,day,hour,minute,seconds; int _size;}b[N];struct directory{ string name; vector< pair<string,int> > hold;//记录当前文件存储的文件,pair表示当前的存储是文件还是目录 map<string,int> is_file; //当前目录下是否存在某个名字的文件 map<string,int> is_directory; //当前目录下是否存在某个目录 string where;//用来记录当前目录的位置 string pre;//上一个文件,用来回到上一个目录 int year,month,day,hour,minute,seconds; int _size;}a[N];vector< pair<string,int> > fid[N];//存储某个名字所有出现的位置,如果second = 0,表示已经被删除map<string,int > file_directory;//找到某个文件或者目录存储的位置int cnt;//表示每个名字对应的位置int cnt1, cnt2;//表示存储的文件和目录的位置int pos = 0;//表示当前所在目录的坐标char place[N];//表示当前位置//1表示目录,0表示文件void manage();//改变字符显示颜色void COLOR_PRINT(const char* s, int color){ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color); printf(s); SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);}void get_systime(int tmp_stt, int tmp_cnt){ //tmp_cnt表示要进行操作的文件或目录的位置 time_t rawtime; struct tm *ptminfo; time(&rawtime); ptminfo = localtime(&rawtime); if(tmp_stt == 1){ //为1表示要对目录时间信息进行更新 a[tmp_cnt].year = ptminfo->tm_year + 1900; a[tmp_cnt].month = ptminfo->tm_mon + 1; a[tmp_cnt].day = ptminfo->tm_mday; a[tmp_cnt].hour = ptminfo->tm_hour; a[tmp_cnt].minute = ptminfo->tm_min; a[tmp_cnt].seconds = ptminfo->tm_sec; } else{ //为0表示要对文件时间信息进行更新 b[tmp_cnt].year = ptminfo->tm_year + 1900; b[tmp_cnt].month = ptminfo->tm_mon + 1; b[tmp_cnt].day = ptminfo->tm_mday; b[tmp_cnt].hour = ptminfo->tm_hour; b[tmp_cnt].minute = ptminfo->tm_min; b[tmp_cnt].seconds = ptminfo->tm_sec; }}//该函数用来获取目录和文件的大小int get_size(int id, int tmp_cnt){//tmp_cnt为目录或文件所在存储位置 int sum = 0; if(id == 1){ //获取目录的大小 sum = a[tmp_cnt].is_file.size() + a[tmp_cnt].is_directory.size(); } else{ //获取文件的大小 for(int i = 0; i < b[tmp_cnt].vet.size(); i++){ sum += b[tmp_cnt].vet[i].size(); } } return sum;}//实现目录打开功能void open(string poss){ //poss表示要打开的目录的名字 if(poss == ".."){ //linux命令行,cd ..表示返回上一级目录 if(pos >= 1) pos --; else{ cout <<"wrong: wrong return!" << endl; } } else{//判断当前目录中是否含有要打开的目录文件 if(a[pos].is_directory.find(poss) == a[pos].is_directory.end()){ cout <<"-bash: cd: " << poss <<" : Not a directory" << endl; } else{ pos = a[pos].is_directory[poss]; } } manage();}//该函数用来实现新建目录功能void mkdir(string poss){ //poss表示所要新建的目录的名字 if(a[pos].is_directory.find(poss) != a[pos].is_directory.end()){ cout <<"warning: duplicate directories!"<< endl; } else{ cnt1 ++; string tmp_pos = a[pos].where; tmp_pos += "/"; tmp_pos += a[pos].name; if(file_directory.find(poss) == file_directory.end()){ cnt ++; file_directory[poss] = cnt; fid[cnt].push_back({tmp_pos,1}); } else{ int x = file_directory[poss]; fid[cnt].push_back({tmp_pos,1}); } a[cnt1].name = poss; a[cnt1].where = tmp_pos; get_systime(1,cnt1); //获取新建目录时的系统时间 a[pos].hold.push_back({poss,1}); a[pos].is_directory[poss] = cnt1; a[cnt1].pre = a[pos].name; } manage();}//该函数用来实现新建文件功能void touch(string name, int stt){ //name表示所要新建的文件名 string tmp_pos = a[pos].where; tmp_pos += "/"; tmp_pos += a[pos].name; if(file_directory.find(name) == file_directory.end()){//查找和判断 cnt ++; file_directory[name] = cnt; fid[cnt].push_back({tmp_pos,1}); } else{ int x = file_directory[name]; fid[x].push_back({tmp_pos,1}); } if(a[pos].is_file.find(name) != a[pos].is_file.end()){ cout <<"warning: duplicate files!" << endl; manage(); } cnt2 ++;//更新相应数据结构 b[cnt2].where =tmp_pos; b[cnt2].name = name; get_systime(0,cnt2); //获取新建文件时的系统时间 a[pos].hold.push_back({name,0}); a[pos].is_file[name] = cnt2; if(stt == 1) manage(); else return;}void echo(string st){//st表示想要写到文件中的信息 string s; s.clear(); string postion; postion.clear(); int i = 0; for(i = 1; i < st.size(); i++){ if(st[i] == '"'){ break; } s += st[i]; } for(i = i + 2; i < st.size(); i++){ postion += st[i]; } if(a[pos].is_file.find(postion) == a[pos].is_file.end()){ //查找所想要写入的文件是否存在,不存在进行创建文件和写入 if(a[pos].is_directory.find(postion) == a[pos].is_directory.end()){ touch(postion, 0); b[cnt2].vet.push_back(s); cout << b[cnt2]._size << endl; } else{ cout <<"wrong: Illegal naming!" << endl; } } else{ //存在的话,直接查询文件位置,然后进行信息写入即可 int x = a[pos].is_file[postion]; b[x].vet.push_back(s); } manage();}//该函数实现正向读取文件内容void cat(string poss){ //poss表示 if(a[pos].is_file.find(poss) == a[pos].is_file.end()){ //如果找不到文件就报错 cout <<"cat " << poss <<": No such file or directory" << endl; } else{//找到文件直接正向读取文件信息即可 int x = a[pos].is_file[poss]; for(int i = 0; i < b[x].vet.size(); i++){ cout << b[x].vet[i] << endl; } } manage();}//该函数实现反向读取文件内容void tac(string poss){ if(a[pos].is_file.find(poss) == a[pos].is_file.end()){ cout <<"cat " << poss <<": No such file or directory" << endl; } else{ int x = a[pos].is_file[poss]; for(int i = b[x].vet.size() - 1; i >= 0; i--){ cout << b[x].vet[i] << endl; } } manage();}void show(string order){ //order表示展示命令 int tmp_tot = 0; if(order == "all"){ //如果展示命令为all,则展示粗略包含信息 for(int i = 0; i < a[pos].hold.size(); i++){ string st = a[pos].hold[i].first; char tmp[M]; memset(tmp, 0, sizeof tmp); for(int j = 0; j < st.size(); j++){ tmp[j] = st[j]; } if(a[pos].hold[i].second == 0){ COLOR_PRINT(tmp,7); tmp_tot ++; cout <<" "; } else{ COLOR_PRINT(tmp,1); tmp_tot ++; cout <<" "; } } if(tmp_tot == 0) cout <<"warning:is null"; cout << endl; } else{ //如果为-l,则展示详细信息,包括建立时间,大小和名称 for(int i = 0; i < a[pos].hold.size(); i++){ string st = a[pos].hold[i].first; char tmp[M]; memset(tmp, 0, sizeof tmp); for(int j = 0; j < st.size(); j++){ tmp[j] = st[j]; } int tmp_pos; if(a[pos].hold[i].second == 1){ if(a[pos].is_directory.find(st) != a[pos].is_directory.end()) tmp_pos = a[pos].is_directory[st]; else continue; if(tmp_tot == 0){ //输出目录的相关信息 printf("create time\t\tsize\tname\n"); } printf("%02d-%02d-%02d %02d:%02d:%02d\t",a[tmp_pos].year,a[tmp_pos].month, a[tmp_pos].day, a[tmp_pos].hour, a[tmp_pos].minute,a[tmp_pos].seconds); cout << get_size(1,tmp_pos)<<'\t'; COLOR_PRINT(tmp,7); tmp_tot ++; cout << endl; } else{ //输出文件的相关信息 if(a[pos].is_file.find(st) != a[pos].is_file.end()) tmp_pos = a[pos].is_file[st]; else continue; if(tmp_tot == 0){ printf("create time\t\tsize\tname\n"); } printf("%02d-%02d-%02d %02d:%02d:%02d\t",b[tmp_pos].year,b[tmp_pos].month, b[tmp_pos].day, b[tmp_pos].hour, b[tmp_pos].minute,b[tmp_pos].seconds); cout << get_size(0,tmp_pos)<<'\t'; COLOR_PRINT(tmp,1); tmp_tot ++; cout << endl; } } if(tmp_tot == 0) cout <<"warning:is null" << endl; } manage();}void find_where(string name){ //寻找名称为name的文件和目录 int tmp_tot = 0; if(file_directory.find(name) == file_directory.end()){ //如果找不到,输出提示信息 cout << "can't find!" << endl; } else{//找到了,遍历进行输出 int x = file_directory[name]; for(int i = 0; i < fid[x].size(); i++){ if(fid[x][i].second == 1){ cout << fid[x][i].first << endl; tmp_tot ++; } } //找不到,输出错误提示信息 if(tmp_tot == 0) cout <<"can't find!" << endl; } manage();}void manage(){ string order1,order2; COLOR_PRINT("/home/user@LAPTOP-MFH2B9V0:",2); string tmp_st = a[pos].where; tmp_st +="/"; tmp_st += a[pos].name; memset(place, 0, sizeof place); for(int i = 0; i < tmp_st.size(); i++){ place[i] = tmp_st[i]; } COLOR_PRINT(place,1); cout << "$ "; cin >> order1 >> order2; if(order1 == "mkdir"){ //新建目录指令 mkdir(order2); } else if(order1 == "touch"){//新建文件指令 touch(order2, 1); } else if(order1 == "cd"){//打开目录/返回上一级目录 open(order2); } else if(order1 == "echo"){//写入信息到文件中去 echo(order2); } else if(order1 == "cat"){//正向读取文件 cat(order2); } else if(order1 == "tac"){//反向读取文件 tac(order2); } else if(order1 == "ls"){//展示目录中所含的文件、子目录信息 show(order2); } else if(order1 == "find"){//按名查找 find_where(order2); } else{ cout <<"command '" << order1 <<"' not found" << endl; //指令不存在报错 } manage();}signed main(){ //一些初始化操作 cout <<"Welcome to OS_work Page (Design Based on Linux)" << endl; cout <<endl; a[0].name = "user"; file_directory["~/home"] = 0; a[0].where = "~/home"; place[0] = '~'; manage(); return 0;}本文作者:fxy本文链接:http://example.com/2022/07/07/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E8%AE%AD-%E5%9F%BA%E4%BA%8E%E5%A4%9A%E7%BA%A7%E7%9B%AE%E5%BD%95%E7%9A%84%E6%96%87%E4%BB%B6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。