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

Thread: Help on fwrite

  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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help on fwrite

    You're opening the file with the "w" flag, which will truncate it to zero length. Use the "a" flag instead. (You will need to use SEEK_SET for your first fseek then, since the current position will be the end of the file after the open.)

  3. #3
    Join Date
    Mar 2009
    Posts
    5

    Re: Help on fwrite

    yes, my mistake. I edit 'w' flag to 'a' flag then the try it again. but unfortunately, even if i use any move method, the new data keep writing to the end of the file ? That's weird!? I then open a new simple project like this:

    PHP Code:
    //first write some bytes into the file
    FILE *fopen("test.bin""wb");
    if(
    == NULL) return 0;
    char *data "Hello!";
    fwrite(datastrlen(data), 1f);

    //close file
    fclose(f);

    //open it again
    fopen("test.bin""ab");

    //reposition cursor to begin of file
    rewind(f);

    //byte to write
    char *"*";

    /
    set cursor pointer 2 bytes far from byte 0.
    fseek
    (f,  2SEEK_SET);      //then SEEK_CUR

    //write *
    fwrite(v11f);

    //close file
    fclose(f); 
    the result is: Hello! *

    in this sample, the asterisk byte always write into the end of the file (after eof byte) while cursor pointer are in the right place when i add a watch to view it? Do i miss something?
    Last edited by dollylamb; March 16th, 2009 at 11:51 PM.

  4. #4
    Join Date
    Apr 2003
    Posts
    1,755

    Re: Help on fwrite

    "a" always appends the data to the file. you should use "r+b" so that you can move the file pointer and write the data correctly.

    Hope it will help you
    Last edited by rxbagain; March 16th, 2009 at 11:57 PM.

  5. #5
    Join Date
    Mar 2009
    Posts
    5

    Re: Help on fwrite

    thanks rxbgain,

    after carefully read msdn, i've notice that r+ is use for "update" mode. Now everything works fine. Thank you for your help.

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