课程设计说明书
设计名称: 操作系统课程设计
题 目: 文件访问接口设计
学生姓名: 陈小浪
专 业: 计算机科学与技术
班 级: 12级1班
学 号: 2012314118
指导教师: 任朝晖
日 期: 20## 年 9 月 15 日
课程设计任务书
计算机科学与技术 专业 年级 班
一、 设计题目
文件访问接口设计
二、 主要内容
利用C语言设计,具体包括:
1、 基本文件内容输入
2、 基本文件内容输出
3、 创建文件
4、 打开文件
5、 关闭文件
6、 文件缓冲区管理
7、 文件句柄管理
8、读顺序文件
9、写顺序文件
10、读随机文件
11、写随机文件
12、文本文件操作验证程序
上述功能由两部分程序验证,中断驻留程序和验证程序。首先运行中断驻留程序,然后运行验证程序得到预期结果。
三、 具体要求
本设计的目的是通过BIOS调用设计简单的文件访问接口,使学生掌握程序接口的设计方法。
要求学生在熟悉比BIOS、DOS操作系统的中断接口及程序接口的基础上,利用C语言设计简单的文件访问接口,最后通过程序验证接口的正确性。
四、 进度安排
依照教学计划,课程设计时间为:2周。
1. 要求讲解、资料查找、系统分析,概要设计 (2天)
2. 系统详细设计、功能设计(2天)
3. 算法实现、编程调试(5天)
4. 功能演示、资料整理、课程设计说明书编写。(1天)
五、 完成后应上交的材料
课程设计说明书纸质文档
六、 总评成绩
指导教师 签名日期 年 月 日
系 主 任 审核日期 年 月 日
目 录
一、程序概述......................................................................................................................................... 1
1.1完成的任务............................................................................................................................... 1
1.2解决的问题............................................................................................................................... 1
二、概念原理......................................................................................................................................... 1
2.1基本概念................................................................................................................................... 1
2.2基本原理................................................................................................................................... 2
三、总体设计......................................................................................................................................... 2
3.1实现方法................................................................................................................................... 2
3.2技术路线................................................................................................................................... 2
四、详细设计......................................................................................................................................... 2
4.1主要函数................................................................................................................................... 2
4.2引用函数................................................................................................................................... 3
五、完成情况......................................................................................................................................... 3
六、使用说明......................................................................................................................................... 3
七、设计总结......................................................................................................................................... 4
7.1系统特色................................................................................................................................... 4
7.2经验教训................................................................................................................................... 5
7.3实践感受................................................................................................................................... 5
参考资料................................................................................................................................................ 6
附 录................................................................................................................................................ 7
一、程序概述
1.1完成的任务
本设计要求编写一个简单的文件访问接口设计,利用C语言,DOS,BIOS中断调用进行设计。主要完成要求中所提到的功能如: 创建文件,删除文件,打开文件,关闭文件,基本文件内容输入,基本文件内容输出,读顺序文件,写顺序文件,文本文件操作验证程序,文件缓冲区管理 的实现。
1.2解决的问题
在设计过程主要遇到了以下的问题:一是关于中断的理解,二是在理解了中断的意义之后,进行中断函数如int86(),int86x(),intdos(),intdos()等函数的调用不是很清晰。
解决的过程:通过与同学讨论,在网上查阅了相关的书籍和学过的课本,如《C高级实用设计》以及《80x86汇编语言程序设计》之后,便有了比较清晰的思路。有关函数的调用参数的设计,基本上是基于这些:结构体WORDREGS,BYTEREGS,SREGS, 联合体REGS ,文件属性字节,以及汇编语言中int 21H dos系统功能调用中中断类型(主要是21H)以及调用功能号的查询和设置。
下面主要介绍上述提及的几个结构体跟联合体:
//以字作为单位的寄存器所组合的结构体
Struct WORDREGS
{
Unsigned int ax,bx,cx,dx,si,di,cflag,flags;
};
//以字节作为单位的寄存器做组合的结构体
Struct BYTEREGS
{
Unsigned char al,ah,bl,bh,cl,ch,dl,dh;
};
//由上述两个结构体所组合的共用体(联合体)
Union REGS
{
Struct WORDREGS x;
Struct BYTEREGS h;
};
//由段寄存器组合的结构体
Struct SREGS
{
Unsigned int es,cs,ss,ds;
};
二、概念原理
2.1基本概念
中断
中断:顾名思义,是指当出现需要时,CPU暂时停止当前程序的执行转而执行处理新情况的程序和执行过程。即在程序运行过程中,系统出现了一个必须由CPU立即处理的情况,此时,CPU暂时中止程序的执行转而处理这个新的情况的过程就叫做中断。本课题中涉及的为dos中断跟bios中断。
文件访问接口
所谓访问接口,实际上即为用户通过dos状态下的提示,键入相关命令后,作为访问相关文件操作的中介,即类似一个中转站。而系统同时也需要在响应了用户键入的命令后执行预先编写好的相应程序,并将程序的执行结果通过中介,即文件访问接口,输出给用户。而事实上在系统响应一次用户的命令时,系统进行了一次IO中断。
中断驻留程序
中断驻留程序,指的是当加载进内存的程序执行完毕后,依然能够保存产生的临时数据跟临时状态,而在下一次调用时继续执行。
验证程序
本课题的验证程序,指的是,当用dos,bios中断调用编写好相关用于文件访问的函数并执行之后,用C语言高级的函数如fopen(),fprintf(),fgetc(),fclose()等函数来验证上述编写的访问函数是否可行。本课题中用fopen(),fclose()来验证用dos中断调用所编写的函数CloseFile(),CreateFile()和CloseFile();用fgetc(),fprintf()等函数来验证用dos中断调用所编写的WriteToFile()和ReadFromFile()函数;除此之外,像ftell(),fseek()等的验证是同样的道理。只需基于上述由dos,bios中断调用所编写的函数.
系统调用
系统调用,顾名思义,说的是操作系统提供给用户程序调用的一组“特殊”接口。用户程序可以通过这组“特殊”接口来获得操作系统内核提供的服务,比如用户可以通过文件系统相关的调用请求系统打开文件、关闭文件或读写文件,可以通过时钟相关的系统调用获得系统时间或设置定时器等。
从逻辑上来说,系统调用可被看成是一个内核与用户空间程序交互的接口,它好比一个中间人,把用户进程的请求传达给内核,待内核把请求处理完毕后再将处理结果送回给用户空间。
2.2基本原理
本课题的运行结果,首先是在运行开始时给定了10个选择开关,供用户选择,每当接收一次用户的选择时,系统将进行一次的IO中断,之后执行相应的中断服务程序,在这里是指由dos,bios中断调用编写好的函数,调用结束时,将临时结果保存,并退出中断,继续响应用户的选择,直到用户选择了退出。
如下图所示:
三、总体设计
3.1实现方法
程序通过选择开关switch...case将用dos,bios调用编写的函数组合起来供用户做出选择。在对应处理用户的选择编写的函数中,主要的核心是dos功能中断调用函数的编写,其中包括中断类型号,中断功能调用号等的设置,以及响应用户选择后中断服务程序的编写。
另外,可以通过参看当前工作目录下的文件信息或者通过Validate开头的相关函数进行程序运行结果的验证
3.2技术路线
整个程序的设计流程,围绕着bios,dos功能中断调用这一主线,编写好相关文件操作函数,在响应用户选择之后,对应执行相关的函数
四、详细设计
4.1主要函数
Int OperOfSel():
用于显示可供用户选择的功能,并提示用户作出相应的功能选择
Void CreateFile(char filename[80]):
用于创建一个新的文件:根据用户输入的文件名进行文件的创建,如果文件创建失败:如磁盘空间已满或者已经存在该文件,即创建失败
Int DeleteFile(char filename[80]):
用于删除一个文件,如果删除成功,将返回文件代号,否则删除失败:可能是不存在该文件
Int OpenFile(char filename[80]):
用于打开一个文件,如果打开成功,则返回文件代号,否则打开失败,可能是不存在该文件
Void CloseFile(filename[80]):
用于关闭一个文件
Void WriteToFile(filename[80]):
用于写顺序文件 基本文件内容的输入 以及文件缓冲区的管理:首先根据用户输入的文件名打开一个文件,如果存在该文件,则打开成功,开始写入内容,此时继续接受用户输入的内容,注意,输入直到用户键入回车键即表示输入完毕,此时将输入的内容根据文件缓冲区管理规则将用户输入的内容进行存储至指定文件名的文件中
Void ReadFromFile(filename[80]):
用于读顺序文件 基本文件内容的输出 以及文件缓冲区的管理:从指定的文件中读取文件中的内容,并输出;
Void ValidateOpenFile(char filename[80]):
用于验证上述文件访问函数编写的正确性:用于验证创建文件 打开文件是否成功
Void ValidateReadFile(char filename[80]):
用于验证上述文件访问函数编写的正确性:用于验证上述中 WriteToFile(filename[80])以及 ReadFromFile(filename[80]):函数
4.2引用函数
Clrscr()
用于清空屏幕的作用
五、完成情况
本程序完成了其中的9项,能够实现文件的创建,打开,关闭,基本文件内容的输入,输出,文件缓冲区的管理,以及文本文件的验证程序,写顺序文件,读顺序文件。因为对文件句柄以及写,读随机文件的理解不是很清晰,故而没有动手实践完成。另外,本程序在dos下进行演示,需要根据提示来进行输入,不允许用户胡乱的输入,不然有可能程序处理不了,即本程序在某种程度上还不是很稳定,安全。但从理论上的角度上讲,只要操作合理,那么本程序将可以实现对应的功能。
六、使用说明
运行成功后进行首页,开始接受用户的选择
接着进行相应功能选项前面对应数字的输入,在这里以1,5,6,10为例,其他为同样的道理
1:为创建一个新的文件
表示test.txt文件已经存在,故而创建失败 查看目录,果然已经存在了test.txt,故而提示创建失败
再次执行1号功能选择:newfile.txt创建成功
查看目录下,果然创建了newfile.txt
进行10号功能的选择
结果将出现清空屏幕的效果
进行5号功能的选择:
输入成功,提示输入了多少个字符 ,上述统计了一下(包括字符)为63个 在目录打开此文件,果然是写入了这些内容
下面进行6号功能的演示
上述的演示结果 正好符合了其中的要求
其他的功能演示也是同样的道理
七、设计总结
7.1系统特色
在本系统中已初步实现一个可访问的文件接口,能够正常的解析命令并执行,在解析命令方面,采用中断函数调用以及功能类型号选择设置的形式,达到直接与底层“打交道”
的作用,效率高
7.2经验教训
在编程过程中要多使用库函数中自带的内容,不必另外再去编写已有的功能,这样不仅能节省时间,也能提高程序的可靠性。要多了解库函数,可以阅读相应的帮助文件来获得有用的信息。遇到问题多与老师同学讨论,可以帮助自己跳出思维定势。
7.3实践感受
每次的课程设计都是对我们所学知识的一个深化,在实践过程中能遇到许多的问题,遇到问题à分析问题à解决问题,这样对知识的理解得到一个升华。实践过程中学到的都是印象非常深刻的,所以以后要多参加这样的实践,认真对待每一次机会。
本次的课程设计,其实从某种程度上来说,还是挺有难度的,因为,之前对于C语言的了解是通过其高级函数的,如文件的访问中,正是通过其高级函数来访问的。刚开始还不知所措,网上的介绍也很模糊,根本很难找到有关dos中断进行文件访问接口的相关设计。后来在于伙伴的讨论下,知道了C高级实用设计这本书,从次书中受益匪浅,加上80x86汇编语言程序设计,更是增长了自己对于汇编的见识,从此更加肯定了汇编在底层操纵的作用!
而且通过此次的课程设计,认识了讨论的重要性,我个人觉得编码正是如此,如果遇到了一个人难以解决,那么最好是提出来与伙伴或者同学一同探讨,这不但能直到别人的向想法,更能让自己认识自己的不足,更有甚者,能够增长自己的见识!!!!!
参考资料
1、《计算机操作系统教程》(第2版)张尧学 史美林 编著 清华大学出版社
2、《操作系统实验指导》任爱华 李鹏 刘方毅 清华大学出版社
3、《操作系统实验教程——核心技术与编程实例》顾宝根 王立松 顾喜梅 科学出版社
4、《80x86汇编语言程序设计》沈美明,温冬婵编著,清华大学出版社
5,、《C高级实用程序设计》王世元 编著 清华大学出版社
6、《C语言程序设计》谭浩强 编著 清华大学出版社
附录:源代码<在TurboC3.0环境>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<dos.h>
#include<bios.h>
//The number of type of interrupt
#define INTER 0x21
//The size of filename
#define Num 80
//the number of characters
#define CNum 9
//Function Of Selection
int OperOfSel()
{
//clrscr();
printf("\n\n\n\n------------Function Of Selection------------\n");
printf("\n0 Exit \n1 Create a new file \n2 Delete a existing file ");
printf("\n3 Open a existing file \n4 Close an existing file ");
printf("\n5 Input basic contents to existing file ");
printf("\n6 Output basic contents from existing file ");
printf("\n7 To validate the file is open or not ");
printf("\n8 To validate the reading contents from file ");
//printf("\n9 To validate the writting contents to file ");
printf("\n10 Clean the screen");
printf("\n\n------------Function Of Selection------------\n");
int sel;
printf("\n--------Input Your Selction: ");
scanf("%d",&sel);
return sel;
}
//Create a new file
void CreateFile(char filename[80])
{
union REGS inregs,outregs;
struct SREGS segregs;
//The function number of create file is 5B
inregs.h.ah=0x5B;
//Set the segment address and effective address
inregs.x.dx=FP_OFF(filename);
segregs.ds=FP_SEG(filename);
//Set the attribute of file
inregs.x.cx=0;
int86x(INTER,&inregs,&outregs,&segregs);
if(outregs.x.ax==2)
{
printf("\n-------Fail To Create New File-----------------\n");
return;
}
if(outregs.x.ax==80)
{
printf("\n-----Fail to create file: %s The file has benn existed-----\n",filename);
return;
}
printf("\n----Create New File:%s Successfully!The File's Attribute is:%d-----\n",filename,outregs.x.cx);
printf("------The FileCode is:%d\n",outregs.x.ax);
}
//Delete a exist file
int DeleteFile(char filename[80])
{
union REGS inregs,outregs;
struct SREGS segregs;
//The function number of create file is 41
inregs.h.ah=0x41;
inregs.x.dx=FP_OFF(filename);
segregs.ds=FP_SEG(filename);
intdosx(&inregs,&outregs,&segregs);
int result = outregs.x.ax;
return result;
}
//Open an existing file
int OpenFile(char filename[80])
{
union REGS inregs,outregs;
struct SREGS segregs;
int result;
inregs.h.ah=0x3D;
inregs.x.dx=FP_OFF(filename);
segregs.ds=FP_SEG(filename);
//Set the way to access file
inregs.h.al=2;
int86x(INTER,&inregs,&outregs,&segregs);
//You Can Through CreateFile() To Know The FileCode as test
//printf("Successfully open file :%d \n",outregs.x.ax);
/*if(2==outregs.x.ax)
{
printf("\n----------Fail To Open The File:%s May not exist\n",filename);
}
else
{
printf("\n----Successfully Opening The File: %s The FileCode Is:%d\n",filename,outregs.x.ax);
}
*/
result = outregs.x.ax;
return result;
}
//Close The Existing File
void CloseFile(char filename[80])
{
union REGS inregs,outregs;
struct SREGS segregs;
int result;
int FileCode;
//To Close The File : We Should Open The File Firstly;The File Should be exist
inregs.h.ah=0x3D;
inregs.x.dx=FP_OFF(filename);
segregs.ds=FP_SEG(filename);
//Set the way to access file
inregs.h.al=2;
int86x(INTER,&inregs,&outregs,&segregs);
//You Can Through CreateFile() To Know The FileCode as test
//printf("Successfully open file :%d \n",outregs.x.ax);
/*if(2==outregs.x.ax)
{
printf("\n----------Fail To Open The File:%s May not exist\n",filename);
}
else
{
printf("\n----Successfully Opening The File: %s The FileCode Is:%d\n",filename,outregs.x.ax);
}
*/
result = outregs.x.ax;
if(2==result)
{
//printf("\n----------Open the file: %s Failly! It may not exist------------\n",filename);
printf("\n-----------Fail to close the file: %s It may not exist-------\n",filename);
}
else
{
//printf("\n-------Open the file: %s Successfully! Now the file will be close----\n",filename);
FileCode = outregs.x.ax;
inregs.h.ah = 0x3E;
inregs.x.bx = FileCode;
int rel = intdos(&inregs,&outregs);
printf("\n-----Close File: %s Successfully! rel=%d -----\n",filename,rel);
}
}
//Input Contents To Existing File
void WriteToFile(char filename[80])
{
union REGS inregs,outregs;
struct SREGS segregs;
int result;
int FileCode;
char WriteContents[CNum];
inregs.h.ah=0x3D;
inregs.x.dx=FP_OFF(filename);
segregs.ds=FP_SEG(filename);
inregs.h.al=2;
int86x(INTER,&inregs,&outregs,&segregs);
result = outregs.x.ax;
if(2==result)
{
printf("\n---Can not input contents to file: %s ! It may not exist------------\n",filename);
}
else
{
//printf("\n-------Open the file: %s Successfully! Now the file will be close----\n",filename);
//---------Very Important Here : To Abosrb The Enter KeyCode;
getchar();
printf("\n\n----Input The Writting Contents:\n");
/*gets(WriteContents);
//printf("WriteContents:%s \n",WriteContents);
//int CharNum=0,i=0;
int i=0;
while(WriteContents[i]!='\0')
{
//CharNum++;
i++;
}
*/
int count=0;
//printf("CharNum=%d i=%d\n",CharNum,i);
//printf("i=%d\n",i);
int temp=0;
int sumChar=0;
for(int i=0;i<=CNum-1;i++)
{
//WriteContents[i] = getchar();
//WriteContents[i] = '';
if(count==1)
{
WriteContents[i]=temp;
i++;
count=0;
}
scanf("%c",&WriteContents[i]);
if(WriteContents[i]=='\n')
{
//printf("i=%d\n",i);
break;
}//if(WriteContents[i]=='\n') end
if(i==CNum-1)
{
//getchar();
//printf("Hello World\n");
//return;
FileCode = result;
inregs.h.ah=0x40;
inregs.x.dx=FP_OFF(WriteContents);
//inregs.x.dx=FP_OFF(WriteContents);
segregs.ds=FP_SEG(WriteContents);
inregs.x.bx=FileCode;
inregs.x.cx=i;
//printf("inregs.x.cx=%d\n",inregs.x.cx);
//int86x(INTER,&inregs,&outregs,&segregs);
intdosx(&inregs,&outregs,&segregs);
//printf("Now The Ax=%d\n",outregs.x.ax);
//printf("inregs.x.bx=%d\n",inregs.x.bx);
//printf("Hello World\n");
temp = WriteContents[i];
count = count+1;
i = -1;
sumChar += outregs.x.ax;
}//if(i==8) end
//printf("%c",WriteContents[i]);
}//end for circle;
/*char TempContents[8];
for(int k=0;k<i;k++)
{
TempContents[k] = WriteContents[k];
}*/
//printf("\n\n--the rest of the contents of file:%s are listed below---\n",filename);
//printf("WriteContents=%s\n",WriteContents);
//printf("WriteContents[0]=%c WriteContents[1]=%c\n",WriteContents[0],WriteContents[1]);
/*for(int j=0;j<i;j++)
{
printf("%c",WriteContents[j]);
}
printf("\n");*/
//printf("end for circle i=%d\n",i);
//printf("WriteContents= %s i=%d\n",WriteContents,i);
//printf("i=%d\n",i);
/*for(int j=0;j<i;j++)
{
printf("%c",WriteContents[j]);
}
printf("\n");*/
FileCode = result;
inregs.h.ah=0x40;
inregs.x.dx=FP_OFF(WriteContents);
segregs.ds=FP_SEG(WriteContents);
inregs.x.bx=FileCode;
inregs.x.cx=i;
//printf("inregs.x.cx=%d\n",inregs.x.cx);
//int86x(INTER,&inregs,&outregs,&segregs);
intdosx(&inregs,&outregs,&segregs);
sumChar += outregs.x.ax;
printf("\n\n---The number of contents you have write to file are: %d--\n",sumChar);
//printf("Now The Ax=%d\n",outregs.x.ax);
//printf("inregs.x.bx=%d\n",inregs.x.bx);
//printf("i=%d\n",i);
}
}
//Read Contents From Existing File;
void ReadFromFile(char filename[80])
{
union REGS inregs,outregs;
struct SREGS segregs;
int result;
char ReadContents[CNum*1024];
inregs.h.ah=0x3D;
inregs.x.dx=FP_OFF(filename);
segregs.ds=FP_SEG(filename);
inregs.h.al=2;
intdosx(&inregs,&outregs,&segregs);
result = outregs.x.ax;
if(2==result)
{
printf("\n---Can not read contents from file: %s ! The file may not exist------------\n",filename);
}
else
{
//printf("\n---Now I'll Read Contents From File: %s\n",filename);
inregs.x.bx=result;
inregs.h.ah=0x3F;
//printf("%d\n",inregs.x.bx);
inregs.x.dx=FP_OFF(ReadContents);
segregs.ds=FP_SEG(ReadContents);
inregs.x.cx=1024;
intdosx(&inregs,&outregs,&segregs);
printf("-----Real Number Of Read Characters in file:%s : %d\n",filename,outregs.x.ax);
//printf("ReadContents= %s\n",ReadContents);
printf("\n-----The Contents Of File: %s Are Listed Below: \n",filename);
for(int i=0;i<outregs.x.ax;i++)
{
printf("%c",ReadContents[i]);
}
printf("\n");
/*for(int i=0;i<1024;i++)
{
printf("%c",ReadContents[i]);
if(ReadContents[i]=='\0')
{
break;
}
}*/
/*int i=0;
while(ReadContents[i]!=NULL)
{
i++;
}
printf("i=%d\n",i);*/
/*for(int j=0;j<i-2;j++)
{
printf("%c",ReadContents[j]);
}
printf("\n");
*/
}
}
//validate function one:open file:after create one file, you can validate the
//file is exist or not!
void ValidateOpenFile(char filename[80])
{
FILE *fp;
fp=fopen(filename,"r");
if(!fp)
{
printf("\n-----Open file: %s Failly! fail to validate open file function-------\n",filename);
return;
}
else
{
printf("\n-----Open file: %s Successfully! success to validate open file function----\n",filename);
}
fclose(fp);
}
//validate function two:read file :after writting contents to one file,you can
//validate the contents whether it has been written to it or not!!
void ValidateReadFile(char filename[80])
{
FILE *fp;
fp = fopen(filename,"r");
if(!fp)
{
printf("\n-------Can not read contents from file:%s It may not exist---------\n",filename);
return;
}
else
{
int count=0;
printf("\n----Read contents from file:%s successfully! The below are the contents----\n",filename);
char ch;
while((ch=fgetc(fp))!=EOF)
{
count++;
printf("%c",ch);
}//while end
printf("\n");
printf("\n-----The total number of contents of the file: %s are: %d---------\n",filename,count);
}//else end
fclose(fp);
}
//Main Function
int main()
{
clrscr();
char CreateFileName[Num];
char DeleteFileName[Num];
char OpenFileName[Num];
char CloseFileName[Num];
char WrittingFileName[Num];
char ReadingFileName[Num];
char ValidateOpenFileName[Num];
char ValidateReadFileName[Num];
int sel;
//printf("Your Selection Is:%d\n",sel);while(sel!=0)
do
{
sel = OperOfSel();
switch(sel)
{
case 0:
//printf("Select 0 Exit\n");
printf("\n-----Exit the system\n");
exit(0);
break;
case 1:
//printf("Select 1\n");
printf("\n--------------Selection Of Creating A NewFile----------------\n");
printf("\n----Input the name of file to create: ");
scanf("%s",CreateFileName);
CreateFile(CreateFileName);
break;
case 2:
//printf("Select 2\n");
printf("\n--------------Selection Of Deleting A ExistingFile-----------------\n");
printf("\n----Input the name of existing file to delete: ");
scanf("%s",DeleteFileName);
//printf("DeleteFileName is: %s",DeleteFileName);
int RelOfDel = DeleteFile(DeleteFileName);
if(0==RelOfDel)
{
printf("\n------The File: %s has been delete successfully\n",DeleteFileName);
}
else
{
printf("\n-----Can not delete the file:%s It may not be exist\n",DeleteFileName);
}
break;
case 3:
printf("\n-----------The Selection Of Opening an Existing File---------\n");
printf("\n----Input the name of existing opening file: ");
scanf("%s",OpenFileName);
//printf("\nOpenFileName=%s\n",OpenFileName);
int RelOfOpenFile = OpenFile(OpenFileName);
if(2==RelOfOpenFile)
{
printf("\n----------Fail To Open The File:%s May not exist\n",OpenFileName);
}
else
{
//printf("\n----Successfully Opening The File: %s The FileCode Is:%d\n",OpenFileName,outregs.x.ax);
printf("\n----Successfully Opening The File: %s\n",OpenFileName);
}
break;
case 4:
printf("\n-----------The Selection Of Closing an Existing File---------\n");
printf("\n------Input The Name Of Closing File: ");
scanf("%s",CloseFileName);
//printf("The CloseFileName=%s\n",CloseFileName);
CloseFile(CloseFileName);
break;
case 5:
printf("\n----------The Selection Of Writting Basic Contents To File--------\n");
printf("----Input The Name Of Writting File : ");
scanf("%s",WrittingFileName);
//printf("WrittingFileName=%s\n",WrittingFileName);
WriteToFile(WrittingFileName);
break;
case 6:
printf("\n----------The Selection Of Reading Basic Contents From File---------\n");
printf("-----Input The Name Of Reading File: ");
scanf("%s",ReadingFileName);
//printf("ReadingFileName= %s\n",ReadingFileName);
ReadFromFile(ReadingFileName);
break;
//printf("\n7 To validate the file is open or not ");
//printf("\n8 To validate the writting contents to file ");
//printf("\n9 To validate the reading contents from file ");
case 7:
printf("\n------------The Selection Of validating the file is open or not-----------\n");
printf("\n---Input the filename to validate the open file function: ");
scanf("%s",ValidateOpenFileName);
ValidateOpenFile(ValidateOpenFileName);
break;
/*case 8:
printf("\n------------The Selection Of validating writting contents to file-----------\n");
break;*/
case 8:
printf("\n------------The Selection Of validating reading contents from file-----------\n");
printf("\n---Input the filename to validate the reading file function: ");
scanf("%s",ValidateReadFileName);
ValidateReadFile(ValidateReadFileName);
break;
case 10:
clrscr();
break;
default:
printf("\n----Input Illegal! Please Re-Input The Aboved Number----\n");
break;
}
}while(sel!=0);
return 0;
}
七、设计总结
7.1系统特色
在本系统中已初步实现一个完整命令处理器的功能,能够正常的解析命令并执行,在解析命令方面,采用指针加数组的形式,既可以节约内存资源,又可以自动扩充命令参数。
7.2经验教训
在编程过程中要多使用库函数中自带的内容,不必另外再去编写已有的功能,这样不仅能节省时间,也能提高程序的可靠性。要多了解库函数,可以阅读相应的帮助文件来获得有用的信息。遇到问题多与老师同学讨论,可以帮助自己跳出思维定势。
7.3实践感受
每次的课程设计都是对我们所学知识的一个深化,在实践过程中能遇到许多的问题,遇到问题à分析问题à解决问题,这样对知识的理解得到一个升华。实践过程中学到的都是印象非常深刻的,所以以后要多参加这样的实践,认真对待每一次机会。
参考资料
1、《计算机操作系统教程》(第2版)张尧学 史美林 编著 清华大学出版社
2、《操作系统实验指导》任爱华 李鹏 刘方毅 清华大学出版社
3、《操作系统实验教程——核心技术与编程实例》顾宝根 王立松 顾喜梅 科学出版社
4、《C++编程思想》 Bruck 著 侯捷译 机械工业出版社
5、《C 语言程序中清除键盘缓冲区的方法》 杨长虹 益阳职业技术学院学报
附 录
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <dir.h>
#include <dos.h>
#include <time.h>
/*定义全局变量*/
char root_dir[3];
char pre_dir[255];
char *cmd_line[255];
char curuser[10];
struct userinf
{
char username[10];
char userpass[10];
};
/*函数申明*/
void init();
int login();
int getcmd();
void dir();
void cd();
void clear();
void newdir();
void deldir();
void del();
void copy();
void cut();
void account();
void help();
main()
{
init();
while(1)/* 消息循环 */
{
switch(getcmd())
{
case 0:
help();
break;
case 1:
dir();
break;
case 2:
cd();
break;
case 3:
newdir();
break;
case 4:
deldir();
break;
case 5:
del();
break;
case 6:
copy();
break;
case 7:
cut();
break;
case 8:
account();
break;
}
}
}
void init()/* 程序初始化 */
{
if(login()==0)
{
exit(0);
}
strcpy(pre_dir,"C:\\");/*设定当前目录*/
clear();/* 清屏 */
printf("S Shell-Above Windows XP [Ver 1.0]\n");
printf("(C) Copyright 20## stars_625.\n\n");
getchar();/* 清空缓冲区 */
}
int login()/* 程序登陆 */
{
char name[10];
char pass[10];
int logintime=3;
FILE *fp;
struct userinf inf;
while(logintime>0)/* 登陆错误超过三次自动退出 */
{
printf("Login:");
scanf("%s",name);
printf("Password:");
scanf("%s",pass);
if((fp=fopen("inf.dll","r"))==NULL)
{
printf("Can't open inf.dll file!\n");
printf("Press any key to exit...");
getch();
exit(0);
}
while(fread(&inf,sizeof(inf),1,fp)==1 && strcmp(inf.username,name)!=0)
{
}
fclose(fp);
if(strcmp(inf.username,name)==0)
{
if(strcmp(inf.userpass,pass)==0)
{
strcpy(curuser,inf.username);
clear();
return 1;
}
else
{
printf("Login error, Press any key to relogin!\n");
getch();
clear();
}
}
else
{
printf("The user is not exist, Press any key to relogin!\n");
getch();
clear();
}
logintime--;
}
printf("Login error above three times, Press any key to exit!");
getch();
return 0;
}
int getcmd()/* 获得命令 */
{
int i=0,j=0,k=0;
char buf[255];
printf("%s>",pre_dir);/* 打印提示符 */
fgets(buf, 255, stdin);
cmd_line[j] = calloc(255, sizeof(char));
while (buf[i] != '\n' && buf[i] != '\0')/* 命令分析 */
{
if (buf[i] != ' ')
{
cmd_line[j][k] = buf[i];
++k;
}
else
{
cmd_line[j + 1] = calloc(255, sizeof(char));
k = 0;
++j;
}
++i;
}
cmd_line[j + 1]=0;
if(strcmp(cmd_line[0],"exit")==0)
{
exit(0);
}
else if(strcmp(cmd_line[0],"/?")==0 || strcmp(cmd_line[1],"/?")==0)
{
return 0;
}
else if(strcmp(cmd_line[0],"dir")==0)
{
return 1;
}
else if(strcmp(cmd_line[0],"cd")==0)
{
return 2;
}
else if(strcmp(cmd_line[0],"newdir")==0)
{
return 3;
}
else if(strcmp(cmd_line[0],"deldir")==0)
{
return 4;
}
else if(strcmp(cmd_line[0],"del")==0)
{
return 5;
}
else if(strcmp(cmd_line[0],"copy")==0)
{
return 6;
}
else if(strcmp(cmd_line[0],"cut")==0)
{
return 7;
}
else if(strcmp(cmd_line[0],"account")==0)
{
return 8;
}
else if(cmd_line[0][1]==':')
{
strcpy(pre_dir,cmd_line[0]);
strcat(pre_dir,"\\");
}
else if(strcmp(cmd_line[0],"clear")==0)
{
clear();
}
else
{
printf("The command is not supported!\n");
}
}
void dir()/* 列出文件及文件夹 */
{
struct ffblk ff;
char filepath[255];
strcpy(filepath,pre_dir);
findfirst(strcat(filepath,"*.*"),&ff,FA_DIREC);
if(ff.ff_attrib==16)
{
printf("<DIR>\t");
}
else
{
printf(" \t");
}
printf("%s\n",ff.ff_name);
while(findnext(&ff)==0)
{
if(ff.ff_attrib==16)
{
printf("<DIR>\t");
}
else
{
printf(" \t");
}
printf("%s\n",ff.ff_name);
}
}
void cd()/* 改变当前目录 */
{
int i=0;
struct ffblk ff;
char filepath[255];
strcpy(filepath,pre_dir);
if(strcmp(cmd_line[1],"..")==0)/* 返回上一层目录 */
{
while(filepath[i]!='\0')
{
i++;
}
if(filepath[i-2]!=':')
{
i=i-2;
while(filepath[i]!='\\' && i>=2)
{
i--;
}
filepath[i+1]='\0';
strcpy(pre_dir,filepath);
}
}
else if(strcmp(cmd_line[1],"\\")==0)/*返回根目录*/
{
while(filepath[i]!='\\')
{
i++;
}
filepath[i+1]='\0';
strcpy(pre_dir,filepath);
}
else
{
findfirst(strcat(filepath,"*.*"),&ff,FA_DIREC);
while(strcmp(ff.ff_name,cmd_line[1])!=0)
{
if(findnext(&ff)!=0)
{
break;
}
}
if(strcmp(ff.ff_name,cmd_line[1])==0)
{
strcat(pre_dir,cmd_line[1]);
strcat(pre_dir,"\\");
}
else
{
printf("Can't find the file!\n");
}
}
void clear()
{
clrscr();
}
void newdir()
{
char filepath[255];
strcpy(filepath,pre_dir);
if(mkdir(strcat(filepath,cmd_line[1]))==0)
{
printf("Make dir '%s' successfully!\n",cmd_line[1]);
}
else
{
printf("Make dir error!\n");
}
}
void deldir()
{
char filepath[255];
strcpy(filepath,pre_dir);
if(rmdir(strcat(filepath,cmd_line[1]))==0)
{
printf("Delete dir '%s' successfully!\n",cmd_line[1]);
}
else
{
printf("Delete dir error!\n");
}
}
void del()
{
char filepath[255];
strcpy(filepath,pre_dir);
if(unlink(strcat(filepath,cmd_line[1]))==0)
{
printf("Delete %s successfully!\n",cmd_line[1]);
}
else
{
printf("Delete error!\n");
}
}
void copy()
{
char filepath[255];
char sourcepath[255];
char aimpath[255];
FILE *newfp;
FILE *oldfp;
char ch;
strcpy(filepath,pre_dir);
if(cmd_line[1][1]!=':')
{
strcpy(sourcepath,filepath);
strcat(sourcepath,cmd_line[1]);
}
else
{
strcpy(sourcepath,cmd_line[1]);
}
if(cmd_line[2][1]!=':')
{
strcpy(aimpath,filepath);
strcat(aimpath,cmd_line[2]);
}
else
{
strcpy(aimpath,cmd_line[2]);
}
if((oldfp=fopen(sourcepath,"r"))==NULL)
{
printf("Can't open old file!\n");
}
if((newfp=fopen(aimpath,"w"))==NULL)
{
printf("Can't creat new file!\n");
}
while((ch=fgetc(oldfp))!=EOF)
{
fputc(ch,newfp);
}
fclose(oldfp);
fclose(newfp);
printf("Copy from %s to %s successfully!\n",sourcepath,aimpath);
}
void cut()
{
char filepath[255];
char sourcepath[255];
char aimpath[255];
FILE *newfp;
FILE *oldfp;
char ch;
strcpy(filepath,pre_dir);
if(cmd_line[1][1]!=':')
{
strcpy(sourcepath,filepath);
strcat(sourcepath,cmd_line[1]);
}
else
{
strcpy(sourcepath,cmd_line[1]);
}
if(cmd_line[2][1]!=':')
{
strcpy(aimpath,filepath);
strcat(aimpath,cmd_line[2]);
}
else
{
strcpy(aimpath,cmd_line[2]);
}
if((oldfp=fopen(sourcepath,"r"))==NULL)
{
printf("Can't open old file!\n");
}
if((newfp=fopen(aimpath,"w"))==NULL)
{
printf("Can't creat new file!\n");
}
while((ch=fgetc(oldfp))!=EOF)
{
fputc(ch,newfp);
}
fclose(oldfp);
fclose(newfp);
if(unlink(sourcepath)==0)
{
printf("Cut from %s to %s successfully!\n",sourcepath,aimpath);
}
else
{
printf("Delete old file error!\n");
}
}
void account()
{
FILE *fp;
struct userinf inf;
if(strcmp(cmd_line[1],"/add")==0)
{
if((fp=fopen("inf.dll","r+"))==NULL)
{
printf("Can't open inf.dll file!\n");
printf("Press any key to exit...");
getch();
}
while(fread(&inf,sizeof(inf),1,fp)==1 && strcmp(inf.username,cmd_line[2])!=0)
{
}
if(strcmp(inf.username,cmd_line[2])==0)
{
printf("Create user error, the user is exist!\n");
}
else
{
strcpy(inf.username,cmd_line[2]);
strcpy(inf.userpass,cmd_line[3]);
if(fwrite(&inf,sizeof(inf),1,fp)==1)
{
printf("Create user %s successfully!\n",inf.username);
}
else
{
printf("Create user error!\n");
}
}
fclose(fp);
}
else if(strcmp(cmd_line[1],"/edit")==0)
{
if((fp=fopen("inf.dll","r+"))==NULL)
{
printf("Can't open inf.dll file!\n");
printf("Press any key to exit...");
getch();
}
while(fread(&inf,sizeof(inf),1,fp)==1 && strcmp(inf.username,cmd_line[2])!=0)
{
}
if(strcmp(inf.username,cmd_line[2])!=0)
{
printf("Edit user error, the user is not exist!\n");
}
else
{
strcpy(inf.username,cmd_line[2]);
strcpy(inf.userpass,cmd_line[3]);
fseek(fp,-20L,1);
if(fwrite(&inf,sizeof(inf),1,fp)==1)
{
printf("Edit user %s successfully!\n",inf.username);
}
else
{
printf("Edit user error!\n");
}
}
fclose(fp);
}
else
{
printf("Please enter correct parameter,type /? for help!\n");
}
}
void help()
{
if(strcmp(cmd_line[0],"/?")==0)
{
printf("The list of commands.\n\n");
printf(" dir\t\tList the files and dirs.\n");
printf(" cd\t\tChange the dir.\n");
printf(" clear\t\tClear the screen.\n");
printf(" newdir\tMake a dir.\n");
printf(" deldir\tDelete a dir.\n");
printf(" del\t\tDelete a file.\n");
printf(" copy\t\tCopy a file from a place to another.\n");
printf(" cut\t\tCut a file from a place to another.\n");
printf(" account\tAdd edit or delete a account.\n\n");
printf("For more information add type /? after command.\n\n");
printf("Notice:All the command line is capitalization distinction!\n\n");
}
else
{
if(strcmp(cmd_line[0],"dir")==0)
{
printf("List the files and dirs.\n\n");
printf("dir path\n\n");
printf(" path\t\tThe dir you want to list.\n");
printf(" \t\tif path is NULL then list the current dir.\n\n");
}
else if(strcmp(cmd_line[0],"cd")==0)
{
printf("Change the dir.\n\n");
printf("cd < \\ | .. | path>\n\n");
printf(" \\\t\tReturn to the root dir.\n");
printf(" ..\t\tReturn to the parent dir.\n");
printf(" path\t\tThe dir you want change to.\n\n");
}
else if(strcmp(cmd_line[0],"clear")==0)
{
printf("Clear the screen.\n\n");
printf("clear\n\n");
}
else if(strcmp(cmd_line[0],"newdir")==0)
{
printf("Make a dir.\n\n");
printf("newdir path\n\n");
printf(" path\t\tThe dir path which you want to create.\n\n");
}
else if(strcmp(cmd_line[0],"deldir")==0)
{
printf("Delete a dir.\n\n");
printf("deldir path\n\n");
printf(" path\t\tThe dir path which you want to delete.\n\n");
}
else if(strcmp(cmd_line[0],"del")==0)
{
printf("Delete a file.\n\n");
printf("del filepath\n\n");
printf(" filepath\tThe filepath which you want to delete.\n\n");
}
else if(strcmp(cmd_line[0],"copy")==0)
{
printf("Copy a file from a place to another.\n\n");
printf("copy source aim\n\n");
printf(" source\tThe source file path.\n");
printf(" aim\t\tThe aim file path.\n\n");
}
else if(strcmp(cmd_line[0],"cut")==0)
{
printf("Cut a file from a place to another.\n\n");
printf("cut source aim\n\n");
printf(" source\tThe source file path.\n");
printf(" aim\t\tThe aim file path.\n\n");
}
else if(strcmp(cmd_line[0],"account")==0)
{
printf("Add edit or delete a account.\n\n");
printf("account < /add | /edit | /del > username userpass\n\n");
printf(" username\tThe account name.\n");
printf(" userpass\tThe account password.\n");
printf(" /add\t\tAdd a account.\n");
printf(" /edit\t\tEdit a account.\n");
printf(" /del\t\tDelete a account.\n\n");
}
else
{
printf("The command is not supported!\n");
}
}
}