CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Help on fwrite

Threaded View

  1. #1
    Join Date
    Mar 2009
    Posts
    5

    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 fopen(myFile"rb");
    DBF_HEADER header;
    DBF_FHEADER *fields;
    int fcount;
    int deleteRec 10;
    char *delMark "*";
    int bytesWritten;

    //read header
    fread(&headersizeof(header), 1f);
    fcount = (header.headerlen 33) / 32;  //get total fields

    //init & read fields header
    fields = new struct DBF_FHEADER[fcount];
    fread(fieldssizeof(DBF_FHEADER), fcountf);

    //move to record 10 & take a look at it.
    fseek(f, (10-1) * header.RecordLenSEEK_CUR);
    char p;
    fread(&psizeof(char), 1f);
    if(
    == "*"){
         
    cout << "Cannot delete a deleted record." << endl;
         return 
    0;
    }
    fclose(f);

    //now start to mark delete given record
    fopen(myFile"wb");

    //move cursor over main header & field header
    fseek(f32 + (32 fcount) + 1SEEK_CUR); 

    //move to given record
    fseek(f, (10 1) * header.RecordLenSEEK_CUR);

    //now write * to the first byte of record string.
    bytesWritten fwrite(delMark11f);
    //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
  •  





Click Here to Expand Forum to Full Width

Featured