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

Reply

Join Date: Jun 2008
Posts: 66
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

 
0
  #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 1:51 am.
Ambarish
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,569
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: 708
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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 Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
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: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 66
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

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 66
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

 
0
  #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 Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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: 749
Team 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

 
0
  #6
Oct 23rd, 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 Thumbnails
Untitled.png  
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 66
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

 
1
  #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. }
Ambarish
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC