当前位置: 代码迷 >> C语言 >> [原创]初学c,自己做的模拟dos
  详细解决方案

[原创]初学c,自己做的模拟dos

热度:440   发布时间:2006-04-24 23:41:00.0
[原创]初学c,自己做的模拟dos

本人是初学者,自己做了一个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让主函数循环 */
}

搜索更多相关的解决方案: dos  初学  模拟  

----------------解决方案--------------------------------------------------------
提示: 该帖被管理员或版主屏蔽

日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-04-24 15:48:00
feng1256

等 级:贵宾
威 望:14
帖 子:2899
专家分:0
注 册:2005-11-24
  得分:0 

我最讨厌别人这么说了。哎………………………………


----------------解决方案--------------------------------------------------------
楼上指哪位,1楼还是2楼?
----------------解决方案--------------------------------------------------------
以下是引用wpan66在2006-4-24 23:41:00的发言:

本人是初学者


----------------解决方案--------------------------------------------------------

下面是我刚学5分钟写的,功能少点

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dir.h>

void Instruction(void);
void RemoveFile(void);
void RenameFile(void);
void CopyFile(void);
void DisplayFile(void);
void DeleteAll(void);
void DeleteFolder(void);

int main(void)
{
int ichoice;

Instruction();
scanf("%d", &ichoice);

while (ichoice != 0)
{
switch (ichoice)
{
case 1:
RemoveFile();
break;
case 2:
RenameFile();
break;
case 3:
CopyFile();
break;
case 4:
DisplayFile();
break;
case 5:
DeleteAll();
break;
case 6:
DeleteFolder();
break;
default:
printf("Disable require.\n");
Instruction();
}
Instruction();
scanf("%d", &ichoice);
}

system("pause");
return 0;
}

void Instruction(void)
{
printf("\n请输入你的选择:0-退出:\n");
printf("1-删除指定的文件.\n");
printf("2-重命名指定的文件.\n");
printf("3-复制指定的文件.\n");
printf("4-显示指定文件的内容.\n");
printf("5-删除指定文件夹中的全部*.*文件.\n");
printf("6-删除某个指定的文件夹和其全部内容,文件夹中不能在包含子文件夹.\n\n");
}

void RemoveFile(void)
{
char filename[30], cha;

printf("请输入要删除文件的路径和文件名:\n");
fflush(stdin);
scanf("%s", filename);

printf("你确定要删除文件吗?输入'y'执行命令,输入'n'取消命令:\n");
fflush(stdin);
cha = getchar();

if (cha == 'y')
{
if ((remove(filename)) == 0)
{
printf("命令执行成功.\n");
}
else
{
printf("命令执行失败.\n");
}
}
else
{
printf("您取消了命令.\n");
}
}

void RenameFile(void)
{
char filenamea[30], filenameb[30], cha;

printf("请输入要重命名的文件路径和文件名:\n");
fflush(stdin);
scanf("%s", filenamea);

printf("请输入新的文件名:\n");
fflush(stdin);
scanf("%s", filenameb);

printf("您确定要执行命令吗?输入'y'-执行命令,输入'n'-取消命令:\n");
fflush(stdin);
cha = getchar();

if (cha == 'y')
{
if ((rename(filenamea, filenameb)) == 0)
{
printf("命令执行成功.\n");
}
else
{
printf("命令执行失败.\n");
}
}
else
{
printf("您取消了命令.\n");
}
}

void CopyFile(void)
{
char filenamea[40], cha ,filenameb[40], ctemp;
FILE *fpa, *fpb;

printf("请输入要复制的文件路径和文件名:\n");
fflush(stdin);
scanf("%s", filenamea);

printf("请输入复制后的文件路径和文件名:\n");
fflush(stdin);
scanf("%s", filenameb);

printf("您确定要执行命令吗?输入'y'-执行命令,输入'n'-取消命令:\n");
fflush(stdin);
cha = getchar();

if ((fpa = fopen(filenamea,"rb")) == NULL) //以只读方式打开2进制文件
{
printf("%s 文件打开失败.\n", filenamea);
system("pause");
exit(1);
}

if ((fpb = fopen(filenameb,"wb")) == NULL)
{
printf("%s 文件创建失败.\n", filenameb);
system("pause");
exit(1);
}

if (cha == 'y')
{
ctemp = fgetc(fpa);
while (!feof(fpa))
{
fputc(ctemp,fpb);
ctemp = fgetc(fpa);
}
fclose(fpa);
fclose(fpb);
}
else
{
printf("你取消了命令.\n");
}
}

void DisplayFile(void)
{
char filename[30], cha, ctemp;
FILE *fpa;

printf("请输入要显示文件的路径和文件名:\n");
fflush(stdin);
scanf("%s", filename);

if ((fpa = fopen(filename,"rb")) == NULL)
{
printf("%s 文件打开失败.\n", filename);
exit(1);
}

printf("您确定要执行命令吗?'y'-执行 命令,'n'-取消命令:\n");
fflush(stdin);
cha = getchar();

if (cha == 'y')
{
ctemp = fgetc(fpa);
while (!feof(fpa))
{
fputc(ctemp,stdout);
ctemp = fgetc(fpa);
}
fclose(fpa);
}
else
{
printf("您取消了命令.\n");
}
}

void DeleteAll(void)
{
char filenamea[30], cha;

printf("请输入要删除内容的文件夹路径和和文件夹名,以\"del \"后跟路径名:\n");
fflush(stdin);
gets(filenamea);

printf("你确定要删除此文件夹中的全部文件吗?'y'-执行命令,'n'取消命令:\n");
fflush(stdin);
cha = getchar();

if (cha == 'y')
{
system(filenamea);
}
else
{
printf("你取消了命令.\n");
}
}

void DeleteFolder(void)
{
char filenamea[30], filenameb[30], cha;

printf("请先删除文件夹中的全部文件:\n");
fflush(stdin);
gets(filenamea);

printf("请再输入要删除的文件夹名:\n");
fflush(stdin);
gets(filenameb);

system(filenamea);
rmdir(filenameb);
}


























----------------解决方案--------------------------------------------------------
原来大家都这么牛,老是出现刚学就能编个什么管理系统甚至解释器等等.
======================================================
其实我什么都不会,但我愿意努力去学.没关系,我有的是耐心.
----------------解决方案--------------------------------------------------------
不能接受吖。感觉自己完蛋了喃。
----------------解决方案--------------------------------------------------------
-_-!6楼怎么把天空弄得这么黑,lz,你的标题是否应该改为模拟dos之简单内存文件镜像系统?你所写的功能远不及dos的百分之一,而且还没有考虑性能
----------------解决方案--------------------------------------------------------
实力这么强还是出学者我晕!那我们这些菜鸟算什么啊!
----------------解决方案--------------------------------------------------------
  相关解决方案