|
-
March 16th, 2009, 08:19 PM
#1
Help on fwrite
Hi guys,
I wrote a program to read DBF file header, content & do some search/delete on records. All others function work fine but delete function doesn't works or exactly it make DBF corrupted after writing deletion mark. The deletion code look like this:
assume that i want to mark delete record number 10
PHP Code:
FILE * f = fopen(myFile. "rb"); DBF_HEADER header; DBF_FHEADER *fields; int fcount; int deleteRec = 10; char *delMark = "*"; int bytesWritten;
//read header fread(&header, sizeof(header), 1, f); fcount = (header.headerlen - 33) / 32; //get total fields
//init & read fields header fields = new struct DBF_FHEADER[fcount]; fread(fields, sizeof(DBF_FHEADER), fcount, f);
//move to record 10 & take a look at it. fseek(f, (10-1) * header.RecordLen, SEEK_CUR); char p; p = fread(&p, sizeof(char), 1, f); if(p == "*"){ cout << "Cannot delete a deleted record." << endl; return 0; } fclose(f);
//now start to mark delete given record f = fopen(myFile, "wb");
//move cursor over main header & field header fseek(f, 32 + (32 * fcount) + 1, SEEK_CUR);
//move to given record fseek(f, (10 - 1) * header.RecordLen, SEEK_CUR);
//now write * to the first byte of record string. bytesWritten = fwrite(delMark, 1, 1, f); //bytesWritten now has value of 1 indicate that write function completed successfully.
fclose(f);
the code above successfully write a byte to dbf file but it also destroys my DBF file (file size after writing task reduced from > 2 MB to ~520KB) anyone knows what happens to my file? please give me an advice. Thanks.
Last edited by dollylamb; March 16th, 2009 at 08:23 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|