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..

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値
}

Recommended Answers

All 6 Replies

>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.

It's absolutely senseless operation to write memory addresses (pointer values) onto external files. Addresses are not persistent values...
Better reread your task specifications...

>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

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..

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.

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..

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値
}
commented: Well done! +23
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.