To delete a file from which data is written document, according to a written into the structure of the body, to remove a specified that you want to delete data which a structure of the body.
Treatment: 1: The original document PWDFILE, first create a temporary file for TEMPFILE back data;
2: First Reading of the cycle PWDFILE, recycling TEMPFILE to write in a structure,
3: If you read that the need to remove the structure, continue to read the source document and not included in the data TEMPFILE,
4: at the end of time to file, fread (& user, sizeof (USER), 1, fp) to return value to 0, this time out.
This put the original document, the structure of the body (of data) in the all-TEMPFILE wrote.
5: at this time and then empty the PWDFILE: fopen (PWDFILE, "w +"), in the form of w + in order to open a file, the file at the same time they clear the
6: TEMPFILE then empty the temporary files after the data PWDFILE wrote in a circle to
7: The last and then empty the TEMPFILE
This deletion of data that the
Special attention: fread () function to read in the successful return to the 1, after the end of the Duchushuoju are not returned to 0 (different compiler is not likely to return to the same value)
Cycle time data, how to jump out of the cycle, the return value of the above problems a night, plus one in the afternoon.
void delete () (
FILE * fp;
USER user;
USER user2;
FILE * newfp;
char chara [10];
char charb [10];
fp = fopen (PWDFILE, "r +");
newfp = fopen (TEMPFILE, "w +");
fseek (newfp, 0,0);
printf ( "del user ..: \ n");
scanf ( "% s", chara);
printf ( "Passwd: \ n");
scanf ( "% s", charb);
while (1) (/ / here a cycle of reading the data in the source file, and a time to write to a temporary file
if (fread (& user, sizeof (USER), 1, fp) == 1) (
if (strcmp (user.name, chara) == 0) (
continue; / / If you need to read the data is deleted data, that is, enter the user name, and file name to match the structure, continue to read the original documents rather than write
) else (/ / to the interim document
fwrite (& user, sizeof (USER), 1, newfp);
)
) else (
break;
)
)
fclose (fp); / / finish after the closure of the source file in order to the next w + in the form of open, empty file
/ / Fseek (newfp,-sizeof (USER), SEEK_END);
fp = fopen (PWDFILE, "w +");
fseek (fp, 0,0); / / source file pointer back to back to the first document (here do not have to have back, because empty document, the indicators point to the head)
fseek (newfp, 0,0); / / When all the data after the TEMPFILE wrote, newfp point to the document at the end of that time must file pointer back to back the interim head
while (1) (
if (fread (& user2, sizeof (USER), 1, newfp) == 1) (
fwrite (& user2, sizeof (USER), 1, fp); / / cycle to a temporary document, wrote all of the data source file
) else (
break;
)
)
fclose (newfp);
fopen (TEMPFILE, "w +"); / / empty the temporary files in order to use the next
fclose (newfp);
);
Output:
del user ..: user1
Passwd: 123456
Pwdfile put this in the user1 users to delete all the data |