/ *********** File management system *********** / # include
INT INIT () / * Initialization Operation Interface Function * / {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 - Catalog Manage" ); Gotoxy (28, 18); Printf ("10 - exit file"); gotoxy (25, 21); Printf ("please choice:"); scanf ("% d", & i); return (i) ; / * Select the corresponding serial number to perform the corresponding operation * /}
Main () {Int x, I, J, Flag = 1; Char Name [15], Name1 [15], Name2 [40]; CHAR Choice, CH; INT HANDLE, STATUS; / * Definition Pointer and Status * / File * fp; while (flash) / * initialization system interface * / {i = init (); getchar (); switch (i) {case 1: label1: / * Create a file operation * / clrs CR (); 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); / * Create a file in the specified file, if there is a file return error code * / if (Handle = = -1) {Printf ("/ nsorry, the file already); getchar (); printf (" / ninput again? (Y or n) "); scanf ("% c ", & cho); getchar ); If (choice == 'y' || goto label1;} else {printf ("/ nthe file is created."); Printf ("Do You now Input CONTENTOF THE? (Y OR N): "); While (1) / * Enter the content of the creation of the file * / {Scanf ("% C ", & cho); if (Choic) E == 'y' || choice == 'N' || choice == 'y' || choice == 'n') Break; Else Printf ("/ Nerror! Please input again!");} Choice == 'y' || choice == 'y') {printf ("/ nnow input content to the file (end with '#'): / n / n"); fp = fopen (name, 'W' ); / * Store the content to the file pointed to by the content * / ch = getchar ();
While (ch! = '#') {FPUTC (CH, FP); CH = getchar ();} fclose (fp); getchar (); / * Close file * /} getchar (); break; casse 2: label2 : / * Delete files * / clrs CL (); gotoxy (25, 5); Printf ("delete file / n"); for (j = 0; j <40; j ) Printf ("="); Printf ("/ N / NPLEASE INPUT THE DLELETING FILE NAME AND ROUTINE: / N"); Scanf ("% s", name); / * Enter the file name to be deleted * / getchar (); printf ("/ n Are you SURE? (Y OR N): "); While (1) {Scanf ("% C ", & cho); if (choice == 'y' || choice == 'n' || khoice == 'y' || choice == 'n') Break; Else Printf ("/ NERROR! PLEASE INPUT AGAIN!");}} (choice == 'y' || choice == 'y') {status = access (name, 0); / * Obtain the status of the file, whether there is * / if (status! = 0) {printf ("/ nsorry the file doesn't exist!"); Getchar (); printf ("/ n / ninput aga (Y or n) "); scanf ("% C ", & choice); getchar (); If (choice == 'y' || goto label2;} else {status = access (name, 02); / * Get the status of the file, whether it exists and reads * / if (status ! = 0) {Printf ("/ nsorry the file is only read!"); Getchar ();} else {unlink (name); / * Remove a file function from the directory, the function is in dos.h * / Printf ("/ N / NDELETE SUCCEFULLY!"); getchar ();
}}} Getchar (); break; case 3: label3: / * Open file operation * / clrs (); gotoxy (27, 5); Printf ("open file / n"); for (j = 0; J < 40; J ) Printf ("="); Printf ("/ n / nplease: / n"); scanf ("% s", name); status = access (name, 0); / * Get the status of the file * / 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' || kittle == 'y') goto label3;} else {printf ("/ nnow begin to read the file: / N "); fp = fopen (name, 'r'); ch = fgetc (fp); / * Read file to memory * / while (ch! = EOF) {Printf ("% C ", CH); CH = FGETC (FP); J ;} fclose (fp); getchar (); / * Close file * /} getchar (); break; case 4: label4: / * write file operation * / CLRSCR (); gotox Y (27, 5); Printf ("Write File / N"); for (j = 0; j <40; j ) Printf ("="); Printf ("/ N / N / N / N / NPLEASE INPUT The Writing File Name and Routine : / n "); scanf ("% s ", name); status = access (name, 0); / * Get file status * / if (status! = 0) {Printf (" / nsorry the file) Doesn't Exist! "); getchar (); Printf (" / n / ninput again? (y or n) "); scanf ("% c ", & cho); getchar (); if (choice == 'y '|| ==' y ') goto label4;
} Else {fp = fopen (Name, 'W'); / * Open Name Pointed in Name * / Printf ("/ NPLEASE INPUT THE INFORMATION (" / NPLEASE INPUT THE INFORMATION); CH = GetChar (); / * Override file * / while (ch! = '#') {FPUTC (CH, FP); ch = getchar ();} fclose (fp); getchar (); / * Close file * / GetChar (); Break; Case 5: Label5: / * Positioning File Operation * / Clrs CR (); 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); / * Get the status of the file pointed to by the name file * / if (status! = 0) {Printf ("/ nsorry the file doesn't exist!"); Getchar (); printf ("/ n / ninput again (y or n ) ")"); Scanf ("% c", & cho); 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); / * Opens the file specified by name, Name can be a simple file name * / / * or the path name of the file, O_CREAT indicates the access code of the file. If the file does not exist, it is created, otherwise it will be invalid. * / / * O_rdwr means that the open file is used to read and write.
S_IREAD | S_IWRITE allows reading and writing * / lseek (Handle, X, Seek_set); / * This function moves the file pointer of the file specified by the handle to seek_set (start position) and add the X offset place * / getchar (); GETCHAR (); Break; Case 6: Label6: / * Modify file attribute operation * / clrs (); gotoxy (27, 5); Printf ("Modify File / N"); for (j = 0; J <80; J ) Printf ("="); Printf ("/ N / N / N / N / N / NAME AND ROUTINE: / N"); Scanf ("% s", name); status = access (name, 0); / * Get the status of the file * / if (status! = 0) {Printf ("/ nsorry the file doesn't exist!); Getchar (); printf (" / n / ninput again (y OR) N) ")"); scanf ("% c", & cho); getchar (); if (choice == 'y' || kittle == 'y') goto label6;} else {printf ("/ NPLEASE choice: 1 --Read_only 2 - write_only "); Printf (" / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / 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); / * Modify the file to "read only" * / if (status) printf ("/ nsorry! couldn't make the file read_only!"); Else Printf ("/ n === Made <% s> read_only ===", name); getchar ();} else if (x ==
2) / * Modify the file to "write" * / {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: clrs (); / * Copy file operation * / gotoxy (27, 5) PRINTF ("Copy File / N"); for (j = 0; j <40; j ) Printf ("="); Printf ("/ N / N / 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 (); struct (name2, "Copy"); strcat (name2, name); strcat (name2, "); strcat (name2, name1); system (name2); / * system call DOS instruction * / getchar (); break; case 8: CLRSCR (); / * Mobile file operation * / 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 (); struct (name2, "move"); strcat (Name2 , Name); strcat (name2, "); strcat (name2, name1); system (name2); / * system call DOS instruction * / getchar ();
Case 9: label9: / * directory management operation * / clrs (); gotoxxy (27, 5); Printf ("Catalog 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 catalog "); gotoxy (25, 12) PRINTF ("2 - Creat Catalog"); Gotoxy (25, 13); Printf ("3 - Detle Catalog"); Gotoxy (25, 14); Printf ("4 - Copy Catalog"; gotoxy); gotoxy 25, 15); Printf ("5 - Move Catalog"); Gotoxy (25, 16); Printf ("6 - EXIT Catalog"); Gotoxy (26, 20); Printf ("Please kice:"); 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 Catalog: / N"); scanf ("% s", name); / * county is a directory operation * / strcpy (Name2, "DIR"); / * Copy Dir Command * / Strcat (Name2, Name); Printf ("% s", Name2); getchar (); system (NAM E2); / * System call * / getChar (); break; case 2: printf ("/ nplease upput the create catalog: / n"); scanf ("% s", name); / * Create a directory operation * / STRCPY (Name2, "MD"); / * Copy MD command * / strcat (name2, name); system (name2); / * System call * / getchar (); break; casse 3: printf ("/ NPLEase Iuput the Deleting catalog: / n "); scanf ("% s ", name); / * Delete directory operation * / strcpy (Name2," RD ");