您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页操作系统实验文件管理C++代码

操作系统实验文件管理C++代码

来源:爱go旅游网
#include <> #include <> #include <> #include <> #include

using namespace std;

#define BLKSIZE 512 .\\n\"); printf(\"WARNING:ALL DATA ON THIS FILESYSTEM WILL BE LOST!\\n\"); printf(\"Proceed with Format(Y/N)\"); scanf(\"%c\

getchar(); if((choice == 'y') || (choice == 'Y')) {

if((fp=fopen(image_name, \"w+b\")) == NULL) {

printf(\"Can't create file %s\\n\ exit(-1); }

for(i = 0; i < BLKSIZE; i++) fputc('0', fp); = 0;

strcpy, \"/\"); = 'd';

strcpy, \"/\"); = 0; = 0; [0] = -1; [1] = -1;

fwrite(&inode, sizeof(Inode), 1, fp); = -1;

for(i = 0; i < 31; i++)

fwrite(&inode, sizeof(Inode), 1, fp); for(i = 0; i < BLKNUM*BLKSIZE; i++) fputc('\\0', fp); fclose(fp); n\

printf(\"This filesystem not exist, it will be create!\\n\"); format(); login(); }

while(!feof(fp)) {

fread(&user, sizeof(User), 1, fp);

1

n\");

flag = 1; fclose(fp); break; } }

if(flag == 0) break; }while(flag); n\ exit(-1); } num = -1; } n\");

exit(-1); } bitmap[i] = '1'; if((fp=fopen(image_name, \"r+b\")) == NULL) {

printf(\"Can't open file %s\\n\ exit(-1); } fseek(fp, i, SEEK_SET); fputc('1', fp); fclose(fp); return i; }

ength; add0 = inode_array[num].address[0]; if(len > 512)

add1 = inode_array[num].address[1]; if((fp = fopen(image_name, \"r+b\")) == NULL) {

printf(\"Can't open file %s.\\n\ exit(-1); } fseek(fp, BLKSIZE+INODESIZE*INODENUM +add0*BLKSIZE, SEEK_SET); ch = fgetc(fp); for(i=0; (i < len) && (ch != '\\0') && (i < 512); i++) {

temp[i] = ch; ch = fgetc(fp); } if(i >= 512)

2

{

fseek(fp,BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE, SEEK_SET); ch = fgetc(fp);

for(; (i < len) && (ch != '\\0'); i++) {

temp[i] = ch;

ch = fgetc(fp); } } temp[i] = '\\0'; fclose(fp); }

ddress[0]; len = inode_array[num].length; if((fp = fopen(image_name, \"r+b\")) == NULL) {

printf(\"Can't open file %s.\\n\ exit(-1); } fseek(fp, BLKSIZE+INODESIZE*INODENUM+add0*BLKSIZE, SEEK_SET); for(i=0; (iadd1 = inode_array[num].address[1];

fseek(fp, BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE, SEEK_SET); for(; (i < len) && (temp[i] != '\\0'); i++) fputc(temp[i], fp); } fputc('\\0', fp); fclose(fp); }

num == 0)

strcpy(path,; else {

strcpy(path,; m=0;

n=inum_cur;

while(m != inum_cur) {

while(inode_array[n].iparent != m) {

n = inode_array[n].iparent;

3

}

strcat(path,\"/\");

strcat(path,inode_array[n].file_name); m = n;

n = inum_cur; } } printf(\"[%s]$\ }

或者 cd dir1) void cd(void) { int i; if(argc != 2) {

printf(\"Command cd must have two args. \\n\"); return ; }

if(!strcmp(argv[1], \"..\"))

inum_cur = inode_array[inum_cur].iparent; else {

for(i = 0; i < INODENUM; i++)

if((inode_array[i].inum>0)&&

(inode_array[i].type=='d')&&

(inode_array[i].iparent==inum_cur)&& !strcmp(inode_array[i].file_name,argv[1])&& !strcmp(inode_array[i].user_name,) break; if(i == INODENUM)

printf(\"This directory isn't exsited.\\n\"); else

inum_cur = i; } } \\n\");

return ; } num> 0) &&

(inode_array[i].iparent == inum_cur)&& !strcmp(inode_array[i].user_name,) {

if(inode_array[i].type == 'd') {

4

dcount++;

printf(\"%-20s

\\n\ } else {

fcount++;

bcount+=inode_array[i].length; printf(\"%-20s%12d bytes\\n\inode_array[i].file_name,inode_array[i].length);

} } printf(\"\\n %d file(s)%11d bytes\\n\ printf(\" %d dir(s) %11d bytes FreeSpace\\n\

}

\\n\"); return ; } num < 0) break;

if(i == INODENUM) {

printf(\"Inode is full.\\n\"); exit(-1); } inode_array[i].inum = i; strcpy(inode_array[i].file_name, argv[1]); inode_array[i].type = 'd'; strcpy(inode_array[i].user_name,; inode_array[i].iparent = inum_cur; inode_array[i].length = 0; save_inode(i); } \\n\");

return ; } for(i = 0; i < INODENUM; i++) {

if((inode_array[i].inum > 0) &&

(inode_array[i].type == 'f') &&

!strcmp(inode_array[i].file_name, argv[1])) {

printf(\"This file is exsit.\\n\"); return ;

5

} } for(i = 0; i < INODENUM; i++)

if(inode_array[i].inum < 0) break; if(i == INODENUM) {

printf(\"Inode is full.\\n\"); exit(-1); } inode_array[i].inum = i; strcpy(inode_array[i].file_name, argv[1]); inode_array[i].type = 'f'; strcpy(inode_array[i].user_name, ; inode_array[i].iparent = inum_cur; inode_array[i].length = 0; save_inode(i); } \\n\");

return ; } for(i = 0; i < INODENUM; i++)

if((inode_array[i].inum > 0) &&

(inode_array[i].type == 'f') &&

!strcmp(inode_array[i].file_name,argv[1])&& !strcmp(inode_array[i].user_name,) break; if(i == INODENUM) {

printf(\"The file you want to open doesn't exsited.\\n\"); return ; } inum = i; printf(\"Please input open mode:(1: read, 2: write, 3: read and write):\"); scanf(\"%d\ getchar(); if((mode < 1) || (mode > 3)) {

printf(\"Open mode is wrong.\\n\"); return; } for(i = 0; i < FILENUM; i++)

if(file_array[i].inum < 0) break; if(i == FILENUM) {

6

printf(\"The file table is full, please close some file.\\n\"); return ; } filenum = i; file_array[filenum].inum = inum;

strcpy(file_array[filenum].file_name, inode_array[inum].file_name); file_array[filenum].mode = mode; printf(\"Open file %s by \ if(mode == 1) printf(\"read only.\\n\"); else if(mode == 2) printf(\"write only.\\n\"); else printf(\"read and write.\\n\"); } \\n\");

return; } for(i = 0; i < FILENUM; i++)

if((file_array[i].inum > 0) &&

!strcmp(file_array[i].file_name,argv[1])) break; if(i == FILENUM) {

printf(\"Open %s first.\\n\ return ; } else if(file_array[i].mode == 2) {

printf(\"Can't read %s.\\n\ return ; } inum = file_array[i].inum; printf(\"The length of %s:%d.\\n\ if(inode_array[inum].length > 0) {

read_blk(inum);

for(i = 0; (i < inode_array[inum].length) && (temp[i] != '\\0'); i++) printf(\"%c\ } } \\n\");

return ; } for(i = 0; i < FILENUM; i++)

if((file_array[i].inum>0)&&

!strcmp(file_array[i].file_name,argv[1])) break;

7

if(i == FILENUM) {

printf(\"Open %s first.\\n\ return ; } else if(file_array[i].mode == 1) {

printf(\"Can't write %s.\\n\ return ; } inum = file_array[i].inum; printf(\"The length of %s:%d\\n\inode_array[inum].length);

if(inode_array[inum].length == 0) {

inode_array[inum].file_name,

i=0;

inode_array[inum].address[0] = get_blknum(); printf(\"Input the data(CTRL+Z to end):\\n\"); while(i<1023&&(temp[i]=getchar())!=EOF) i++; temp[i]='\\0'; length=strlen(temp)+1;

inode_array[inum].length=length; if(length > 512)

inode_array[inum].address[1] = get_blknum(); save_inode(inum); write_blk(inum); } else

printf(\"This file can't be written.\\n\"); } \\n\");

return ; } for(i = 0; i < FILENUM; i++)

if((file_array[i].inum > 0) &&

!strcmp(file_array[i].file_name, argv[1])) break; if(i == FILENUM) {

printf(\"This file doesn't be opened.\\n\"); return ; } else {

8

file_array[i].inum = -1;

printf(\"Close %s success!\\n\ } }

num = -1; if(inode_array[i].length > 0) {

release_blk(inode_array[i].address[0]); if(inode_array[i].length >= 512)

release_blk(inode_array[i].address[1]); } save_inode(i); }

\\n\");

return ; } int n,t,i; stack istk;

for(i = 0; i < INODENUM; i++)num >=0) &&

(inode_array[i].iparent == inum_cur)&& (!strcmp(inode_array[i].file_name,argv[1]))&& (!strcmp(inode_array[i].user_name,)) {

n=inode_array[i].inum; break; }

if(i==INODENUM) puts(\"Directory ERROR\"); else {

(n);

while(!()) {

t=(); (); del(t);

for(i = 0; i < INODENUM; i++)

if((inode_array[i].inum >=0) &&(inode_array[i].iparent == t))

(i); }

} }

// 功能: 退出当前用户(logout)

9

void logout() { printf(\"Do you want to exit this user(y/n)\"); scanf(\"%c\ getchar(); if((choice == 'y') || (choice == 'Y')) {

printf(\"\\nCurrent user has exited!\\n\"); login(); } return ; }

// 功能: 退出文件系统(quit) void quit() { printf(\"Do you want to exist(y/n):\"); scanf(\"%c\ getchar();

if((choice == 'y') || (choice == 'Y')) exit(0); }

// 功能: 显示错误 void errcmd() { printf(\"Command Error!!!\\n\"); }

//清空内存中存在的用户名 void free_user() { int i; for(i=0;i<10;i++) [i]='\\0'; }

// 功能: 循环执行用户输入的命令, 直到logout

// \"help\\"delete\

void command(void) { char cmd[100]; system(\"cls\"); do {

pathset();

10

gets(cmd);

switch(analyse(cmd)) {

case 0:

help(); break; case 1:

cd(); break; case 2:

dir(); break; case 3: mkdir(); break; case 4: create(); break; case 5: open(); break; case 6: read(); break; case 7: write(); break; case 8: close(); break; case 9: delet(); break; case 10: logout();break; case 11: system(\"cls\");break; case 12: format(); init(); free_user(); login();break; case 13: quit(); break; case 14: errcmd(); break; default: break; } }while(1); }

// 主函数

int main(void) {

11

login(); init(); command(); return 0; }

12

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务