There is a C language topic requires deletion of data in the file. According to I know that the C language seems to have a function of the data in the file? This can only be considered to open the relevant documentation (R, W, A, R , W , A ) and FREAD (), fwrite () and other functions implement the deleted operation! First, read the data in the file to a pointer structure, because the data size in the file cannot be determined, so the structural array cannot be used. The use of the pointer structure is to dynamically allocate storage space using malloc (). Then read the data into this structure, then filter the data to write back to the file. Since we have to take out the data and length in the file first, so Open mode of the file Select R: Read the data but not delete (no R because only reading is not written, do not choose W, W because the data is deleted? Do not choose A, A does not need reason), data After reading, it is the operation of rewriting, and the file open mode selection W. Struct info {char name [30]; char addr [50]; long telno;} * temp, client;
Void del () {file * fp1, * fp2, * fp3; int count = 0; char name [30]; if ((fp1 = fopen ("TELEFON.DAT", "R")) == null) {Printf ("Error Open Telefon.dat File"); Return;} While ((& Client, Struct Info), 1, FP1)) == 1) Count = 1; // Statistics Temp = (Struct Info * ) Malloc (Struct * (Struct Info)); // Assign Storage Space REWIND (FP1); FREAD (TEMP, SIZEOF (Struct Info), Count, FP1); // Read data in the file to the pointer structure FCLOSE (FP1); if ((fp2 = fopen ("TELEFON.DAT", "W") == NULL) / / Delete the original file, create an empty file for the same name for writing {Printf ("Error Create New Telefon .dat file "); Return;} else {fclose (fp2); fp3 = fopen (" telefon.dat "," a ") Printf (" / T / N / NENTER SUBSCRIBER NAME (UPTO 30 Chars): "); Scanf ("% s", & name); // Specify the delete object for (int i = 0; i