一、简单的DOS功能
编一程序,实现几个简单的DOS功能,包括:mtype、mcopy、mcomp等。
mtype 实现DOS的type功能,显示.TXT文件的内容
mcopy 实现DOS的copy功能,复制一个任意格式的磁盘文件。
mcomp 实现DOS的comp功能,比较二个任意格式的磁盘文件,从长度、内容二个方面。
1.注意用带参数的main函数。
2.只能用c语言完成,不允许用c++
其实也就是文件操作喽, 我帮你完成,希望对你有启发 1.mtype #include <stdio.h>
int main(int argc, char *argv[]) { FILE *fp; char ch; switch(argc) { case 1: printf("Error! Please input the file name!\nExample: mtype OUT.txt\n"); return 0; break; case 2: if((fp = fopen(argv[argc-1], "r")) == 0) { printf("No such File,Please check!"); return 0; } while(!feof(fp)) { ch = getc(fp); putchar(ch); } break; default: printf("Error!You can only type one file one time\nExample: mtype OUT.txt\n"); return 0; break; } fclose(fp); return 1; } 完成后命名为mtype.c,然后编译,找到mtype.exe,这样输入mtype 11.txt回车 11.txt是你要打印出来的文件 注意:此程序只能打印文本文件
2,mcopy #include <stdio.h>
int main(int argc, char *argv[]) { FILE *fin, *fout; char ch;
switch(argc) { case 1: case 2: printf("Error! Please input the file name!\nExample: mcopy in.txt out.txt\n"); return 0; break; case 3: if((fin = fopen(argv[argc-2], "rb")) == 0) { printf("No such File,Please check!"); return 0; } if((fout = fopen(argv[argc-1], "wb")) == 0) { printf("Disk error,Can not create file or can not open file!"); return 0; } while(!feof(fin)) { ch = getc(fin); putc(ch, fout); } break; default: printf("Error! too many parameters!\nExample: mcopy in.txt out.txt\n"); return 0; break; } fclose(fin); fclose(fout); printf("Copy Successfully!"); return 1; } 注意:此程序只能把一个文件的内容拷贝给另一个文件,为任意格式
3,mcomp #include <stdio.h>
int main(int argc, char *argv[]) { FILE *fpL, *fpR; char chL, chR; switch(argc) { case 1: case 2: printf("Error! Please input the file name!\nExample: mcomp 1.txt 2.txt\n"); return 0; break; case 3: if((fpL = fopen(argv[argc-2], "rb")) && (fpR = fopen(argv[argc-1], "rb"))) { do { chL = getc(fpL); chR = getc(fpR); } while(!feof(fpL) && !feof(fpR) && (chL == chR)); } else { printf("Can not open file, please check!"); return 0; } break; default: printf("Error! too many parameters!\nExample: mcomp 1.txt 2.txt\n"); return 0; break; } fclose(fpL); fclose(fpR); if((chL-chR) < 0) printf("Left file < Right file!"); else if((chL-chR) > 0) printf("Left file > Right file!"); else printf("Left file = Right file!"); return 1; }