/***********文 件 管 理 系 统***********/ #include <stdio.h> #include <stdlib.h> /*不容易归类的标准函数库*/ #include <conio.h> #include <string.h> #include <sys\stat.h> #include <fcntl.h> /*非标准文件输入输出操作的代码符号属性*/ #include <dos.h> #include <io.h>
int init() /*初始化操作界面函数*/ {int i; clrscr(); gotoxy(23,4);printf("* * * * * * * * * * * * * *"); gotoxy(27,5);printf("FILE MANAGE SYSTEM"); gotoxy(23,6);printf("* * * * * * * * * * * * * *"); gotoxy(28,9);printf("1--Creat File"); gotoxy(28,10);printf("2--Delete File"); gotoxy(28,11);printf("3--OPen File"); gotoxy(28,12);printf("4--Write File"); gotoxy(28,13);printf("5--Locate File"); gotoxy(28,14);printf("6--Modify File"); gotoxy(28,15);printf("7--Copy File"); gotoxy(28,16);printf("8--Move File"); gotoxy(28,17);printf("9--Cataloge Manage"); gotoxy(28,18);printf("10--Exit File"); gotoxy(25,21); printf("Please Choice:"); scanf("%d",&i); return(i); /*选择相应的序号,执行相应的操作*/ }
main() {int x,i,j,flag=1; char name[15],name1[15],name2[40]; char choice,ch; int handle,status; /*定义文件的指针和状态*/ FILE *fp; while(flag) /*初始化系统界面*/ {i=init(); getchar(); switch(i) { case 1:label1: /*创建文件操作*/ clrscr(); gotoxy(27,5);printf("CREAT FILE\n"); for(j=0;j<40;j++) printf("= "); printf("\n\nPlease input the creating file name and routine:\n"); scanf("%s",name); getchar(); handle=creatnew(name,0);/*按指定的文件方式创建文件,若有同名文件返回错误代码*/ if(handle==-1) { printf("\nSorry,the file already exists."); getchar(); printf("\nInput again?(Y or N)"); scanf("%c",&choice);getchar(); if(choice=='Y'||choice=='y') goto label1; } else { printf("\nThe file is created."); printf("Do you now input contentof the file?(Y or N):"); while(1) /*输入创建文件的内容*/ { scanf("%c",&choice); if(choice=='y'||choice=='n'||choice=='Y'||choice=='N') break; else printf("\nError!Please input again!"); } if(choice=='y'||choice=='Y') { printf("\nNow input content to the file(End with '#'):\n\n"); fp=fopen(name,'w');/*把内容存放到fp指向的文件中去*/ ch=getchar(); while(ch!='#') { fputc(ch,fp); ch=getchar(); } fclose(fp);getchar();/*关闭文件*/ } getchar(); break; case 2:label2: /*删除文件的操作*/ clrscr(); gotoxy(25,5);printf("DELETE FILE\n"); for(j=0;j<40;j++) printf("= "); printf("\n\nPlease input the deleting file name and routine:\n"); scanf("%s",name); /*输入要删除的文件名*/ getchar(); printf("\n Are you sure?(Y or N):"); while(1) { scanf("%c",&choice); if(choice=='y'||choice=='n'||choice=='Y'||choice=='N') break; else printf("\nError!Please input again!"); } if(choice=='y'||choice=='Y') {status=access(name,0);/*获取文件的状态,是否存在*/ if(status!=0) {printf("\nSorry the file doesn't exist!"); getchar(); printf("\n\nInput again?(Y or N)"); scanf("%c",&choice);getchar(); if(choice=='Y'||choice=='y') goto label2; } else { status=access(name,02);/*获取文件的状态,是否存在并且是否只读*/ if(status!=0) { printf("\nSorry the file is only read!"); getchar(); } else {unlink(name); /*从目录中删除一个文件函数,该函数在dos.h中*/ printf("\n\ndelete succefully!"); getchar(); } } } getchar(); break; case 3:label3: /*打开文件操作*/ clrscr(); gotoxy(27,5);printf("OPEN FILE\n"); for(j=0;j<40;j++) printf("= "); printf("\n\nPlease input the opening file name and routine:\n"); scanf("%s",name); status=access(name,0);/*获取文件的状态*/ if(status!=0) {printf("\nSorry the file doesn't exist!"); getchar(); printf("\n\nInput again?(Y or N)"); scanf("%c",&choice);getchar(); if(choice=='Y'||choice=='y') goto label3; } else { printf("\nNow begin to read the file:\n"); fp=fopen(name,'r'); ch=fgetc(fp); /*读出文件到内存*/ while(ch!=EOF) {printf("%c",ch); ch=fgetc(fp);j++; } fclose(fp);getchar();/*关闭文件*/ } getchar(); break; case 4:label4: /*写文件操作*/ clrscr(); gotoxy(27,5);printf("WRITE FILE\n"); for(j=0;j<40;j++) printf("= "); printf("\n\nPlease input the writing file name and routine:\n"); scanf("%s",name); status=access(name,0);/*获取name指向的文件状态*/ if(status!=0) {printf("\nSorry the file doesn't exist!"); getchar(); printf("\n\nInput again?(Y or N)"); scanf("%c",&choice);getchar(); if(choice=='Y'||choice=='y') goto label4; } else {fp=fopen(name,'w');/*以写入方式打开name 指向的文件*/ printf("\nPlease input the information(end with '#'):\n"); ch=getchar(); /*重写文件*/ while(ch!='#') { fputc(ch,fp); ch=getchar(); } fclose(fp);getchar();/*关闭文件*/ } getchar(); break; case 5:label5: /*定位文件操作*/ clrscr(); gotoxy(27,5);printf("LOCATE FILE\n"); for(j=0;j<40;j++) printf("= "); printf("\n\nPlease input the locating file name and routine:\n"); scanf("%s",name); status=access(name,0);/*获取name文件指向的文件的状态*/ if(status!=0) {printf("\nSorry the file doesn't exist!"); getchar(); printf("\n\nInput again?(Y or N)"); scanf("%c",&choice);getchar(); if(choice=='Y'||choice=='y') goto label5; } else {printf("\nPlease input the location:"); scanf("%d",&x); handle=open(name,O_CREAT|O_RDWR,S_IREAD|S_IWRITE);/*打开由name指定的文件,name既可以是简单的文件名*/ /*也可以是文件的路径名,O_CREAT表示了打开文件的存取代码,若文件不存在,则建立,否则无效。*/ /*O_RDWR表示打开文件用于读写。S_IREAD|S_IWRITE允许读写*/ lseek(handle,x,SEEK_SET);/*该函数把由handle指定的文件的文件指针,移到SEEK_SET(开始位置)再加上x偏移量的地方*/ getchar(); } getchar(); break; case 6:label6: /*修改文件属性操作*/ clrscr(); gotoxy(27,5);printf("MODIFY FILE\n"); for(j=0;j<80;j++) printf("= "); printf("\n\nPlease input the modifying attribution file name and routine:\n"); scanf("%s",name); status=access(name,0);/*获取文件的状态*/ if(status!=0) {printf("\nSorry the file doesn't exist!"); getchar(); printf("\n\nInput again?(Y or N)"); scanf("%c",&choice);getchar(); if(choice=='Y'||choice=='y') goto label6; } else { printf("\nPlease choice:1--READ_ONLY 2--WRITE_ONLY"); printf("\n\nPlease choice the attributione operation:"); while(1) { scanf("%d",&x); if(x==1||x==2) break; else printf("\nError!Please input again!"); } if(x==1) { status=chmod(name,S_IREAD);/*修改文件为“只读”*/ if(status) printf("\nSorry!Couldn't make the file read_only!"); else printf("\n===Made <%s> read_only===",name); getchar(); } else if(x==2) /*修改文件为“只写”*/ { status=chmod(name,S_IWRITE); if(status) printf("\nSorry!Couldn't make the file write_only!"); else printf("\n===Made <%s> write_only===",name); getchar(); } } getchar(); break; case 7:clrscr(); /*复制文件的操作*/ gotoxy(27,5);printf("COPY FILE\n"); for(j=0;j<40;j++) printf("= "); printf("\n\nPlease input the copying file name and routine:\n"); scanf("%s",name); getchar(); printf("\nPlease input the copyed file name and routine:\n"); scanf("%s",name1); getchar(); strcpy(name2,"copy "); strcat(name2,name); strcat(name2," "); strcat(name2,name1); system(name2); /*系统调用dos指令*/ getchar(); break; case 8:clrscr(); /*移动文件操作*/ gotoxy(27,5);printf("MOVE FILE\n"); for(j=0;j<40;j++) printf("= "); printf("\n\nPlease input the moving file name and routine:\n"); scanf("%s",name); getchar(); printf("\nPlease input the moving file name and routine:\n"); scanf("%s",name1); getchar(); strcpy(name2,"move "); strcat(name2,name); strcat(name2," "); strcat(name2,name1); system(name2); /*系统调用dos指令*/ getchar(); break; case 9: label9: /*目录管理操作*/ clrscr(); gotoxy(27,5);printf("CATALOGUE MANAGE\n"); for(j=0;j<40;j++) printf("= "); gotoxy(13,9); printf("Please input the moving file name and routine:\n"); gotoxy(25,11);printf("1--display catalogue"); gotoxy(25,12);printf("2--creat catalogue"); gotoxy(25,13);printf("3--detele catalogue"); gotoxy(25,14);printf("4--copy catalogue"); gotoxy(25,15);printf("5--move catalogue"); gotoxy(25,16);printf("6--exit catalogue"); gotoxy(26,20); printf("Please choice:"); scanf("%d",&x); while(x<1||x>6) {printf("\nError!Please input again!\n"); scanf("%d",&x); } switch(x) { case 1: printf("\nPlease iuput the displaying catalogue:\n"); scanf("%s",name);/*县是目录操作*/ strcpy(name2,"dir ");/*复制dir命令*/ strcat(name2, name); printf("%s",name2); getchar(); system(name2);/*系统调用*/ getchar(); break; case 2: printf("\nPlease iuput the creating catalogue:\n"); scanf("%s",name);/*创建目录操作*/ strcpy(name2,"md ");/*复制md命令*/ strcat(name2,name); system(name2);/*系统调用*/ getchar(); break; case 3: printf("\nPlease iuput the deleting catalogue:\n"); scanf("%s",name);/*删除目录操作*/ strcpy(name2,"rd ");/*复制rd命令*/ strcat(name2,name); system(name2); getchar(); break; case 4: printf("\nPlease iuput the copying catalogue:\n"); scanf("%s",name);/*复制目录操作*/ printf("\nPlease iuput the displayed catalogue:\n"); scanf("%s",name1); strcpy(name2,"xcopy ");/*复制xcopy命令*/ strcat(name2,name); strcat(name2," "); strcat(name2,name1); strcat(name2,"/e"); system(name2);/*系统调用*/ getchar();break; case 5: printf("\nPlease iuput the moving catalogue:\n"); scanf("%s",name);/*移动目录操作*/ printf("\nPlease iuput the moved catalogue:\n"); scanf("%s",name1); strcpy(name2,"move ");/*复制move命令*/ strcat(name2,name); strcat(name2," "); strcat(name2,name1); system(name2); getchar();break; case 6:goto tag; /*退出目录管理操作*/ } printf("Input again?(Y or N)"); scanf("%c",&choice);getchar(); if(choice=='Y'||choice=='y') goto label9; tag:getchar(); break; case 10:flag=0;exit(0);break; /*退出文件管理系统程序*/ default: clrscr(); printf("\n\n Error!Please input again!\n"); getchar(); break; } } } }

|