RSS Forums RSS

Help: fwrite, I want to write forward pointer address in present file pointer

Please support our C advertiser: Programming Forums
Reply
Posts: 62
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Help: fwrite, I want to write forward pointer address in present file pointer

  #1  
Oct 22nd, 2008
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..
  1. void create_fbeCodeDat()
  2. {
  3. int checkSum=0;
  4. fwrite(&signature,1, 4, bin_fp);//signature=0xabcd0001
  5. fwrite(&version, 1,4,bin_fp);//Version番号
  6. fseek(bin_fp,4,SEEK_CUR); //FBD Program code
  7. fseek(bin_fp,2,SEEK_CUR); //not used
  8. fwrite(&fbdStepno,1,2,bin_fp); //FBD step No.[N1]
  9. fseek(bin_fp,4,SEEK_CUR); //const data offset
  10. //Here i have to write the address of next fbdstepno*4+1 bytes
  11. //consider fbdstepno=4, here in 4 bytes have to write address of 5th byte address
  12. // for ex: next 5th byte adress has to be write here in 4 bytes
  13. //fwrite( addr(5th byte), 1,4, bin_fp); //how to write 1st argument
  14. // 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
  15. // for buf=0 and 1, have to write bufflist[0].fbCodeAddr in 4 bytes, and bufflist[1].fbCodeAddr in 4 bytes
  16. //and so on... and next the following code continues..
  17.  
  18. for(int buf=0; buf<buffList.size();++buf)
  19. {
  20. fwrite(buffList[buf].fbCodeAddr,1,buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr, bin_fp);
  21. }
  22. fwrite(&checkSum,1,4,bin_fp);//sum値
  23. }
Last edited by ambarisha.kn : Oct 22nd, 2008 at 12:51 am.
Ambarish
AddThis Social Bookmark Button
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help: fwrite, I want to write forward pointer address in present file pointer

  #2  
Oct 22nd, 2008
>i want to write forward pointer address in present pointer field.
If I understand your problem correctly, you have two immediate options:
  1. Get the current address, then offset it by the known amount and write it.
  2. Seek forward, save the current address, then seek back and write it.
I'm here to prove you wrong.
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help: fwrite, I want to write forward pointer address in present file pointer

  #3  
Oct 22nd, 2008
It's absolutely senseless operation to write memory addresses (pointer values) onto external files. Addresses are not persistent values...
Better reread your task specifications...
Reply With Quote  
Posts: 62
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Re: Help: fwrite, I want to write forward pointer address in present file pointer

  #4  
Oct 22nd, 2008
Originally Posted by Narue View Post
>i want to write forward pointer address in present pointer field.
If I understand your problem correctly, you have two immediate options:
  1. Get the current address, then offset it by the known amount and write it.
  2. Seek forward, save the current address, then seek back and write it.



How to get the current address
Ambarish
Reply With Quote  
Posts: 62
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Re: Help: fwrite, I want to write forward pointer address in present file pointer

  #5  
Oct 22nd, 2008
How to get the current address. Actually first i leave some empty space to write the offset address.
After writing data from buffer, i have to calculate the offset address and have to write back the offset address..

i will give you the picture, how i have to write dat file.
see atachment..
Attached Files
File Type: doc datfile.doc (81.5 KB, 5 views)
Ambarish
Reply With Quote  
Posts: 5,126
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 633
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help: fwrite, I want to write forward pointer address in present file pointer

  #6  
Oct 22nd, 2008
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.
Attached Images
File Type: png Untitled.png (21.0 KB, 5 views)
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!
Reply With Quote  
Posts: 62
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Re: Help: fwrite, I want to write forward pointer address in present file pointer

  #7  
Oct 23rd, 2008
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..
  1. void create_fbeCodeDat()
  2. {
  3. int checkSum=0;
  4. unsigned int totBufSize=0;
  5. vector<unsigned int>offset;
  6. fwrite(&signature,1, 4, bin_fp);//signature=0xabcd0001
  7. fwrite(&version, 1,4,bin_fp); //Version番号
  8. fseek(bin_fp,4,SEEK_CUR); //FBDプログラムコード, FBDパラメータサイズ
  9. fseek(bin_fp,2,SEEK_CUR); //未使用(未定義)
  10. fwrite(&fbdStepno,1,2,bin_fp); //FBDステップ数[N1]
  11. fseek(bin_fp,4,SEEK_CUR); //定数データへのオフセット
  12. fseek(bin_fp,fbdStepno*4,SEEK_CUR); //FBDステップ オフセット
  13.  
  14. unsigned int fbdStepOff=fbdStepno*4;//initial constant
  15. offset.push_back(fbdStepOff); //Offset for FBD step 0
  16.  
  17. /*ブッファにあるFB毎の実行コードの追加*/
  18. for(int buf=0; buf<buffList.size();++buf)
  19. {
  20. //offset for fbd steps 1 to (n-1), adding size and initial constant
  21. fbdStepOff+= buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr;
  22. offset.push_back(fbdStepOff);
  23. fwrite(buffList[buf].fbCodeAddr,1,buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr, bin_fp);
  24. //printf("Buff FBCODEADDr:0x%x Buff tmpAdr:0x%x\n", buffList[buf].fbCodeAddr, buffList[buf].tmpAddr);
  25. totBufSize+=buffList[buf].tmpAddr+4-buffList[buf].fbCodeAddr;
  26. }
  27. fseek(bin_fp, -(totBufSize+fbdStepno*4), SEEK_CUR);
  28. for(int off=0;off<offset.size();++off)
  29. {
  30. fwrite(&offset[off], 1,4,bin_fp);
  31. }
  32. fseek(bin_fp, totBufSize, SEEK_CUR);
  33. fwrite(&checkSum,1,4,bin_fp); //sum値
  34. }
  35.  
Ambarish
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C Forum
Views: 753 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:35 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC