943,761 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1491
  • C RSS
Oct 22nd, 2008
0

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

Expand Post »
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.
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Oct 22nd, 2008
1

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 22nd, 2008
0

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

It's absolutely senseless operation to write memory addresses (pointer values) onto external files. Addresses are not persistent values...
Better reread your task specifications...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 22nd, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Narue ...
>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
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Oct 22nd, 2008
0

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

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, 29 views)
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Oct 23rd, 2008
0

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

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
Click image for larger version

Name:	Untitled.png
Views:	32
Size:	21.0 KB
ID:	7888  
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 23rd, 2008
1

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

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. }
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Retrieving a paragraph instead of a single line of text
Next Thread in C Forum Timeline: Help with C code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC