本人是初学者,自己做了一个dos模拟系统,可提供文件建立,删除,修改,加密,复制,粘贴,格式化盘功能,目前未实现的就是无法建立文件夹。请各位高手测试,指点。如果有建立文件夹的方法或找出bug,请指点一下.
我的qq:251859754
/*****************************
File Manager Virtual System
******************************/
#include "stdio.h"
#include "string.h"
#include <time.h>
int TIME=0; /*记录即时时间*/
int w=0; /*w=1表示Node *cpy已指向复制内容,w=0表示Node *cpy未指向复制内容 */
unsigned char sCurDate[30];
void SendCentreTime(void)
{
struct tm *current_date;
time_t seconds;
time(&seconds);
current_date = localtime(&seconds);
memset(sCurDate, 0 , 100);
sprintf(sCurDate, "%04d-%02d-%02d %02d:%02d:%02d",current_date->tm_year+1900,
current_date->tm_mon+1, current_date->tm_mday,
current_date->tm_hour, current_date->tm_min,current_date->tm_sec);
}
typedef struct Node
{
char name[100];
char content[100];
int size;
char time[30];
char code[15];
struct Node *next;
}Node,*LinkList;
Node *cpy; /* 指向复制内容的指针,其被使用后不用释放 */
LinkList CreateFromTail()
{ /* 尾插法建空链表 */
LinkList L;
L=(Node*)malloc(sizeof(Node));
L->next=NULL;
return(L);
}
createF(LinkList L)
{ /*在链表末尾建立新文件,即在虚拟盘中建立新文件*/
Node *s,*r,*p;
r=L;
while(r->next!=NULL)
{
r=r->next;
}
s=(Node*)malloc(sizeof(Node));
printf("input name:");
p=L;
gets(s->name);
while(p!=NULL) /*该循环保证命名无重复*/
{
if(strcmp(p->next->name,s->name)==0)
{
printf("The file named '%s' is exist,please change:",s->name);
p=L;
gets(s->name);
continue;
}
p=p->next;
}
printf("input content:");
gets(s->content);
printf("input password:");
gets(s->code);
strcpy(s->time,sCurDate);
s->size=Size(s->content);
r->next=s;
s->next=NULL;
}
printfLinkList(LinkList h)
{ /*打印虚拟盘中的所有文件*/
h=h->next;
printf("********************************************\nFIle size Time Code\n");
while(h!=NULL)
{ printf("%s ",h->name);
printf("%d ",h->size);
printf("%s ",h->time);
printf("%s\n",h->code); /*实际应用中密码打印应去掉*/
h=h->next;
}
printf("\n********************************************\n");
}
DelF(LinkList L)
{
Node *r,*s;
char delname[20];
char pw[15];
printf("input file name needs delete:");
gets(delname);
r=L;
while(strcmp(r->next->name,delname)!=0&&r->next!=NULL)
{
r=r->next;
}
if(r->next==NULL) /*需删除文件不存在*/
{
printf("File '%s' is not exist!\n",delname);
}
else
{
printf("input the password of File '%s':",delname);
gets(pw);
if(strcmp(r->next->code,pw)==0)
{
s=r->next->next;
free(r->next); /*释放删除的文件结点*/
r->next=s;
printf("File '%s' is already delete!\n",delname);
}
else printf("Password error,you can't delete it!\n");
}
}
OpenF(LinkList L)
{
Node *r;
char openname[20];
char pw[15];
printf("input file name needs open:");
gets(openname);
r=L;
while(strcmp(r->next->name,openname)!=0&&r->next!=NULL)
{
r=r->next;
}
if(r->next==NULL) /*需访问文件不存在*/
{
printf("File '%s' is not exist!\n",openname);
}
else
{
printf("input the password of File '%s':",openname);
gets(pw);
if(strcmp(r->next->code,pw)==0)
printf("***************************\n%s\n***************************\n",r->next->content);
else printf("Password error!\n");
}
}
WriteF(LinkList L)
{
Node *r;
char writename[20],pw[15];
printf("input file name needs write:");
gets(writename);
r=L;
while(strcmp(r->next->name,writename)!=0&&r->next!=NULL)
{
r=r->next;
}
if(r->next==NULL) /*需写操作文件不存在*/
{
printf("File '%s' is not exist!\n",writename);
}
else
{
printf("input the password of File '%s':",writename);
gets(pw);
if(strcmp(r->next->code,pw)==0)
{
printf("input new content:");
gets(r->next->content);
printf("input new password:");
gets(r->next->code);
strcpy(r->next->time,sCurDate);
r->next->size=Size(r->next->content);
printf("File '%s' write is finished!\n",writename);
}
else printf("password error!\n");
}
}
Size(char a[100])
{
int i=0;
while(a[i]!='\0')
{
i++;
}
return(i);
}
copyF(LinkList L)
{
Node *r;
char openname[20];
char pw[15];
printf("input file name needs copy:");
gets(openname);
r=L;
while(strcmp(r->next->name,openname)!=0&&r->next!=NULL)
{
r=r->next;
}
if(r->next==NULL) /*需访问文件不存在*/
{
printf("File '%s' is not exist!\n",openname);
}
else
{
printf("input the password of File '%s':",openname);
gets(pw);
if(strcmp(r->next->code,pw)==0)
{
cpy=r->next;
w=1;
}
else printf("Password error!\n");
}
}
pasteF(LinkList L)
{
Node *s,*r,*p;
if(w==0)
{
printf("The space of copy is empty!\n");
return;
}
r=L;
while(r->next!=NULL)
{
r=r->next;
}
s=(Node*)malloc(sizeof(Node));
p=L;
strcpy(s->name,cpy->name);
while(p!=NULL) /*该循环保证命名无重复*/
{
if(strcmp(p->next->name,s->name)==0)
{
printf("The file named '%s' is exist,please change:",s->name);
p=L;
gets(s->name);
continue;
}
p=p->next;
}
strcpy(s->content,cpy->content);
printf("input new password:");
gets(s->code);
strcpy(s->time,sCurDate);
s->size=Size(s->content);
r->next=s;
s->next=NULL;
printf("1 file(s) copied!\n");
}
FormatF(LinkList L)
{
Node *h;
h=L;
h=h->next;
while(h!=NULL)
{
free(h); /*释放个节点,不占用系统空间*/
h=h->next;
}
L->next=NULL;
printf("format is finish!\n");
}
Help()
{
printf(" -----------------------------------------------\n");
printf(" | HELP CONTENT |\n");
printf(" | |\n");
printf(" |-----------------------------------------------|\n");
printf(" | create file:create | delete file:del |\n");
printf(" |-----------------------------------------------|\n");
printf(" | open file:open | write file:write |\n");
printf(" |-----------------------------------------------|\n");
printf(" |format:format;format d:| device screen:cls |\n");
printf(" |-----------------------------------------------|\n");
printf(" | watch disk:dir ; dir d: |\n");
printf(" |-----------------------------------------------|\n");
printf(" | copy file:copy | paste file:paste |\n");
printf(" |-----------------------------------------------|\n");
printf(" | help:help |soft imformation:about |\n");
printf(" |-----------------------------------------------|\n");
printf(" | change another disk:D:| exit:exit |\n");
printf(" -----------------------------------------------\n");
}
about()
{
printf(" -----------------------------------------------\n");
printf(" | File Manager Virtual System |\n");
printf(" | |\n");
printf(" |-----------------------------------------------|\n");
printf(" | |\n");
printf(" | (c)All Right Reserved PW |\n");
printf(" | |\n");
printf(" | Turbo C 2.0 |\n");
printf(" | version 2006 1.0 |\n");
printf(" | |\n");
printf(" | Name:Pan Wei |\n");
printf(" | Sno:20031160141 |\n");
printf(" | Department:CS |\n");
printf(" | E-mail:wpan66@126.com |\n");
printf(" -----------------------------------------------\n");
}
main()
{
LinkList C,D,E,L; /* 定义三个虚拟外存盘,分别为C,D,E */
char order[20];
int choice=0,choice1=0;
C=CreateFromTail();
D=CreateFromTail();
E=CreateFromTail();
printf("\n\n\n\n");
about();
printf("\n\n\n\n");
printf("Press any key to continue...");
getch();
clrscr();
printf("You can use 'help'!\nC:\\");
while(1)
{
SendCentreTime(); /*计时器*/
gets(order);
if(strcmp(order,"C:")==0||strcmp(order,"c:")==0)
{
choice=0; /*将当前盘换为C:\盘操作*/
}
else if(strcmp(order,"D:")==0||strcmp(order,"d:")==0)
{
choice=1; /*将当前盘换为D:\盘操作*/
}
else if(strcmp(order,"E:")==0||strcmp(order,"e:")==0)
{
choice=2; /*将当前盘换为E:\盘操作*/
}
else if(strcmp(order,"")==0) printf(""); /*对当前盘不执行操作*/
else if(strcmp(order,"cls")==0) clrscr(); /*清屏*/
else if(strcmp(order,"help")==0) Help(); /*帮助*/
else if(strcmp(order,"about")==0) about(); /*关于*/
else if(strcmp(order,"exit")==0) exit(0); /*退出*/
else if(strcmp(order,"dir")==0)
{
choice1=1; /*访问当前盘,调用函数printfLinkList(LinkList L)*/
if (choice==0) printf("Directory of C:\\ \n");
else if (choice==1) printf("Directory of D:\\ \n");
else if (choice==2) printf("Directory of E:\\ \n");
}
else if(strcmp(order,"dir c:")==0||strcmp(order,"dir C:")==0)
{
choice=0; choice1=1; /*访问c盘,调用函数printfLinkList(C)*/
printf("Directory of C:\\ \n");
}
else if(strcmp(order,"dir d:")==0||strcmp(order,"dir D:")==0)
{
choice=1; choice1=1; /*访问d盘,调用函数printfLinkList(D)*/
printf("Directory of D:\\ \n");
}
else if(strcmp(order,"dir e:")==0||strcmp(order,"dir E:")==0)
{
choice=2; choice1=1; /*访问e盘,调用函数printfLinkList(E)*/
printf("Directory of E:\\ \n");
}
else if(strcmp(order,"create")==0) choice1=2; /*在当前盘建立文件,调用函数creatF(LinkList L)*/
else if(strcmp(order,"open")==0) choice1=3; /*在当前盘访问文件,调用函数OpenF(LinkList L)*/
else if(strcmp(order,"del")==0) choice1=4; /*在当前盘删除文件,调用函数DelF(LinkList L)*/
else if(strcmp(order,"write")==0) choice1=5; /*在当前盘执行写操作,调用函数WriteF(LinkList L)*/
else if(strcmp(order,"copy")==0) choice1=6; /*在当前盘执行复制操作,调用函数copyF(LinkList L)*/
else if(strcmp(order,"paste")==0) choice1=7; /*在当前盘执行粘贴操作,调用函数pasteF(LinkList L)*/
else if(strcmp(order,"format")==0)
{
choice1=8; /*格式化当前盘,调用函数FormatF(LinkList L)*/
}
else if(strcmp(order,"format c:")==0||strcmp(order,"format C:")==0)
{
choice=0; choice1=8; /*格式化c盘*/
}
else if(strcmp(order,"format d:")==0||strcmp(order,"format D:")==0)
{
choice=1; choice1=8; /*格式化d盘*/
}
else if(strcmp(order,"format e:")==0||strcmp(order,"format E:")==0)
{
choice=2; choice1=8; /*格式化e盘*/
}
else printf("'%s' is bad command!\n",order);
if(choice==0)
{
switch(choice1)
{
case 0:break;
case 1:printfLinkList(C);break;
case 2:createF(C);break;
case 3:OpenF(C);break;
case 4:DelF(C);break;
case 5:WriteF(C);break;
case 6:copyF(C);break;
case 7:pasteF(C);break;
case 8:FormatF(C);break;
}
printf("C:\\");
}
if(choice==1)
{
switch(choice1)
{
case 0:break;
case 1:printfLinkList(D);break;
case 2:createF(D);break;
case 3:OpenF(D);break;
case 4:DelF(D);break;
case 5:WriteF(D);break;
case 6:copyF(D);break;
case 7:pasteF(D);break;
case 8:FormatF(D);break;
}
printf("D:\\");
}
if(choice==2)
{
switch(choice1)
{
case 0:break;
case 1:printfLinkList(E);break;
case 2:createF(E);break;
case 3:OpenF(E);break;
case 4:DelF(E);break;
case 5:WriteF(E);break;
case 6:copyF(E);break;
case 7:pasteF(E);break;
case 8:FormatF(E);break;
}
printf("E:\\");
}
choice1=0;
} /*while让主函数循环 */
}