?? ms dos.c
字號:
#include<stdio.h>
#include<dir.h>
#include<string.h>
#include<conio.h>
#include<errno.h>
#include<dos.h>
#include<io.h>
#include<process.h>
#define PRF printf
char buffer[MAXPATH], str1[12];
char file[MAXPATH];
/*buffer用來存儲工作目錄, 定義為全局變量,在用cd..和cd命令有用*/
makefile()/*建立一個新文件*/
{
int handle;
scanf("%s", file);
if((strcmp(file, "/?") == 0)
{
printf("mf newfilename");
}
else
{
handle = creatnew(file, 0);
if(handle == -1)
printf("%s already exists.\n", file);
else
printf("%s successfully created.\n", file);
}
}
dir()/*列出當前的工作目錄中的文件和目錄*/
{
char para[6];
scanf("%s", para);
if(strcmp(para, "/?" == 0)
PRF(" dir [/b][/m][/p][|more]\n";
else
if(strcmp(para, "/w" == 0)system("dir/w";
else if(strcmp(para, "/b" == 0)system("dir/b";
else if(strcmp(para, "|more" == 0)system("dir|more";
else if(strcmp(para, "/b|more" == 0)system("dir/b|more";
else if(strcmp(para, "/w|more" == 0)system("dir/w|more";
else if(strcmp(para, "/p" == 0)system("dir/p";
else PRF("'dir' have no '%s' parameter", para);
}
del()/*刪除文件*/
{
struct ffblk ffblk; /*定義一個搜索文件的結構體*/
int done, flag=0, i;
scanf("%s", file);
for(i=0; i<strlen(file); i++)
if(file[i] == '?' || file[i] == '*')
{
flag=1;
break;
}
if(strcmp(file, "/?" == 0)
PRF(" del filename del *.extendname";
else if(flag)/*刪除一特定文件*/
{
done=findfirst(file, &ffblk, 0);
while(!done)
{
remove(ffblk.ff_name); /*刪除找到符合條件的文件*/
done=findnext(&ffblk);
}
PRF("All files that accord to your conditions were deleted!\n";
}
else
{
if(remove(file) == 0)
PRF("Removed %s successfully!\n", file);
else
perror("remove"; /*perror()是c語言里的錯誤處理函數*/
}
}
cd()/*進入選定目錄*/
{
char dirname[MAXPATH];
scanf("%s", dirname);
if(strcmp(dirname, "/?" == 0)
PRF(" cd directoryname";
else
{
strcpy(buffer, dirname); /*將輸入的目錄名賦給buffer*/
if(chdir(buffer))/*更改工作目錄*/
perror("chdir()";
getcwd(buffer, MAXPATH); /*用函數chdir()更改目錄后,重新給buffer賦值,使其為當前目錄*/
}
}
cdup()/*返回到上層目錄, 有點小問題拉*/
{
char str_1[MAXPATH], *p;
int n;
strcpy(str_1, buffer);
n=strlen(str_1); /*n存儲工作目錄buffer字符串的長度*/
p=str_1+n; /*使指針指向字符串的最后一個字符*/
while((*p) != '\\')
p--;
*p='\0'; /*在指針指向str_1的地方重新賦值*/
strcpy(buffer, str_1);
if(chdir(buffer))/*更改工作目錄*/
perror("chdir()";
getcwd(buffer, MAXPATH); /*用函數chdir()更改目錄后,重新給buffer賦值,使其為當前目錄*/
}
cls()/*清屏*/
{
clrscr();
}
ver()/*版本信息,純屬好玩*/
{
PRF("This programme is created by mabiqiang!\nVersion 1.0 2004-4-14";
}
type()/*顯示文件內容*/
{
FILE *fp;
struct ftime ft; /*文件被最后修改的時間日期信息*/
char ch;
int y; /*定義屏幕上橫坐標x和縱坐標y*/
scanf("%s", file);
if(strcmp(file, "/?" == 0)
PRF(" type filename(include extend name)";
else
{
if((fp=fopen(file, "r") == NULL)
PRF("cannot open %s", file);
while(!feof(fp))
{
ch=fgetc(fp);
PRF("%c", ch);
y=wherey(); /*取得當前的縱坐標值*/
if(y == 25)/*屏幕的高度是25行*/
{
printf("\n--------more--------";
/*當滿屏顯示后就暫停,直到用戶按下任意鍵后,再繼續顯示以下的內容*/
getch();
clrscr();
}
}
PRF("\n\n This file was modified at ";
getftime(fileno(fp), &ft); /*取得文件最后修改的時間日期信息*/
PRF("%u:%u:%u ", ft.ft_hour, ft.ft_min, ft.ft_tsec*2);
PRF("%u/%u/%u\n", ft.ft_month, ft.ft_day, ft.ft_year+1980);
fclose(fp);
}
}
edit()/*編輯一已存在的文件(其實不存在也沒關系), 有一點小問題就是不能夠修改,只能增加數據*/
{
FILE *fp;
char ch1, ch2;
scanf("%s", file);
if(strcmp(file, "/?" == 0)
PRF(" edit filename(include extend name)";
else
{
if((fp=fopen(file, "a+") == NULL)
PRF("cannot open %s", file);
while(!feof(fp))
{
ch1=fgetc(fp);
PRF("%c", ch1);
}
getchar(); /*清除緩存中的回車字符*/
ch2=getchar();
while(ch2 != '$')
{
fputc(ch2, fp);
ch2=getchar();
}
fclose(fp);
}
}
copy()/*復制文件*/
{
FILE *in, *out;
char ch, infile[MAXPATH], outfile[MAXPATH];
scanf("%s", infile);
scanf("%s", outfile);
if((in=fopen(infile, "r") == NULL)
PRF("cannot open %s", infile);
if((out=fopen(outfile, "w+") == NULL)
PRF("cannot open %s", outfile);
while(!feof(in))
fputc(fgetc(in), out);
fclose(in);
fclose(out);
PRF(" 1 files was copied!";
}
ren()/*改文件名*/
{
char oldname[MAXPATH], newname[MAXPATH];
scanf("%s", oldname); /*原文件名*/
scanf("%s", newname); /*新文件名*/
if(rename(oldname, newname) == 0)
PRF("Renamed %s to %s successfully!\n", oldname, newname);
else
perror("rename";
}
mdir()/*建立目錄*/
{
int status;
char dirname[MAXPATH];
scanf("%s", dirname);
if(strcmp(dirname, "/?" == 0)
PRF(" md directoryname\n";
else
{
status = mkdir(dirname);
(!status)?(PRF("Directory created successfully!\n")PRF("Unable to create directory\n");
}
}
rdir()/*刪除目錄*/
{
int status;
char dirname[MAXPATH];
scanf("%s", dirname);
if(strcmp(dirname, "/?" == 0)
PRF(" rd directoryname\n";
else
{
status = rmdir(dirname);
(!status)?(PRF("Directory deleted successfully!\n")perror("Unable to delete directory");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -