Help: fwrite, I want to write forward pointer address in present file pointer
Please support our C advertiser: Programming Forums
![]() |
•
•
Posts: 62
Reputation:
Solved Threads: 0
I wrote the following function and it is working fine, but my problem is i want to write forward pointer address in present pointer field.
I will explain in code.
please check line numbers 10, 11, 12, 13 in the following code..
I will explain in code.
please check line numbers 10, 11, 12, 13 in the following code..
c Syntax (Toggle Plain Text)
void create_fbeCodeDat() { int checkSum=0; fwrite(&signature,1, 4, bin_fp);//signature=0xabcd0001 fwrite(&version, 1,4,bin_fp);//Version番号 fseek(bin_fp,4,SEEK_CUR); //FBD Program code fseek(bin_fp,2,SEEK_CUR); //not used fwrite(&fbdStepno,1,2,bin_fp); //FBD step No.[N1] fseek(bin_fp,4,SEEK_CUR); //const data offset //Here i have to write the address of next fbdstepno*4+1 bytes //consider fbdstepno=4, here in 4 bytes have to write address of 5th byte address // for ex: next 5th byte adress has to be write here in 4 bytes //fwrite( addr(5th byte), 1,4, bin_fp); //how to write 1st argument // actually in the program here i have to write the address of buffList[buf].fbCodeAddr in 4 bytes. for buf=0, have to write bufflist[0].fbCodeAddr in 4 bytes // for buf=0 and 1, have to write bufflist[0].fbCodeAddr in 4 bytes, and bufflist[1].fbCodeAddr in 4 bytes //and so on... and next the following code continues.. for(int buf=0; buf<buffList.size();++buf) { fwrite(buffList[buf].fbCodeAddr,1,buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr, bin_fp); } fwrite(&checkSum,1,4,bin_fp);//sum値 }
Last edited by ambarisha.kn : Oct 22nd, 2008 at 12:51 am.
Ambarish
>i want to write forward pointer address in present pointer field.
If I understand your problem correctly, you have two immediate options:
If I understand your problem correctly, you have two immediate options:
- Get the current address, then offset it by the known amount and write it.
- Seek forward, save the current address, then seek back and write it.
I'm here to prove you wrong.
•
•
Posts: 62
Reputation:
Solved Threads: 0
•
•
•
•
>i want to write forward pointer address in present pointer field.
If I understand your problem correctly, you have two immediate options:
- Get the current address, then offset it by the known amount and write it.
- Seek forward, save the current address, then seek back and write it.
How to get the current address
Ambarish
It's an array of offsets into the file, not addresses as such.
Since you know how many there are, you can calculate the size of this table, and populate it with what the effective start positions of each buffList[buf].fbCodeAddr will be written to (it's just an initial constant, and a sum of lengths)
Oh, and try to use portable image formats in future.
Since you know how many there are, you can calculate the size of this table, and populate it with what the effective start positions of each buffList[buf].fbCodeAddr will be written to (it's just an initial constant, and a sum of lengths)
Oh, and try to use portable image formats in future.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
•
•
Posts: 62
Reputation:
Solved Threads: 0
Thanks Salem, I got it..
Actually i was confused with offset and address. Now i got it what is offset.
My code is working fine..
Code is as follows.
please suggest me if there is any better idea than the following code..
Actually i was confused with offset and address. Now i got it what is offset.
My code is working fine..
Code is as follows.
please suggest me if there is any better idea than the following code..
C Syntax (Toggle Plain Text)
void create_fbeCodeDat() { int checkSum=0; unsigned int totBufSize=0; vector<unsigned int>offset; fwrite(&signature,1, 4, bin_fp);//signature=0xabcd0001 fwrite(&version, 1,4,bin_fp); //Version番号 fseek(bin_fp,4,SEEK_CUR); //FBDプログラムコード, FBDパラメータサイズ fseek(bin_fp,2,SEEK_CUR); //未使用(未定義) fwrite(&fbdStepno,1,2,bin_fp); //FBDステップ数[N1] fseek(bin_fp,4,SEEK_CUR); //定数データへのオフセット fseek(bin_fp,fbdStepno*4,SEEK_CUR); //FBDステップ オフセット unsigned int fbdStepOff=fbdStepno*4;//initial constant offset.push_back(fbdStepOff); //Offset for FBD step 0 /*ブッファにあるFB毎の実行コードの追加*/ for(int buf=0; buf<buffList.size();++buf) { //offset for fbd steps 1 to (n-1), adding size and initial constant fbdStepOff+= buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr; offset.push_back(fbdStepOff); fwrite(buffList[buf].fbCodeAddr,1,buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr, bin_fp); //printf("Buff FBCODEADDr:0x%x Buff tmpAdr:0x%x\n", buffList[buf].fbCodeAddr, buffList[buf].tmpAddr); totBufSize+=buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr; } fseek(bin_fp, -(totBufSize+fbdStepno*4), SEEK_CUR); for(int off=0;off<offset.size();++off) { fwrite(&offset[off], 1,4,bin_fp); } fseek(bin_fp, totBufSize, SEEK_CUR); fwrite(&checkSum,1,4,bin_fp); //sum値 }
Ambarish
![]() |
Other Threads in the C Forum
- Previous Thread: Retrieving a paragraph instead of a single line of text
- Next Thread: Help with C code
•
•
•
•
Views: 753 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode