-
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.
-
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.)
-
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 *f = fopen("test.bin", "wb");
if(f == NULL) return 0;
char *data = "Hello!";
fwrite(data, strlen(data), 1, f);
//close file
fclose(f);
//open it again
f = fopen("test.bin", "ab");
//reposition cursor to begin of file
rewind(f);
//byte to write
char *v = "*";
/set cursor pointer 2 bytes far from byte 0.
fseek(f, 2, SEEK_SET); //then SEEK_CUR
//write *
fwrite(v, 1, 1, f);
//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?
-
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 :)
-
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.