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

Dani AI

Generated

Short summary and what to watch for
As pointed out, you must not write raw memory pointer values into a file — they are meaningless across runs and machines. What you actually want is an array of file offsets (as explained). 's two options (compute offsets ahead, or reserve and fill later) are both valid; implemented the reserve-then-fill approach. Below are suggestions to make that approach robust, portable and easier to maintain.

A compact, reliable workflow

  • Use fixed-width integer types for headers and offsets (uint16_t, uint32_t, uint64_t) and write using sizeof rather than hard-coded byte counts.
  • Reserve the offset table, write payloads while pushing offsets computed from the file position, then seek back and overwrite the reserved table.
  • Prefer C++ streams (tellp/seekp) or, in C, use ftello/fseeko for large files instead of ftell/fseek.

Example (different from the thread’s FILE* code; shows the pattern):

// sketch: reserve table, record offsets, then fill table
std::ofstream ofs("out.bin", std::ios::binary);
uint32_t signature = 0xABCD0001;
ofs.write(reinterpret_cast<const char*>(&signature), sizeof signature);

// reserve N uint32_t offsets
auto table_pos = ofs.tellp();
uint32_t zero = 0;
for (size_t i=0;i<N;++i) ofs.write(reinterpret_cast<const char*>(&zero), sizeof zero);

// write buffers and record offsets
std::vector<uint32_t> offsets;
for (const auto& buf : buffers) {
    offsets.push_back(static_cast<uint32_t>(ofs.tellp()));
    ofs.write(reinterpret_cast<const char*>(buf.data()), buf.size());
}

// write offsets back
ofs.seekp(table_pos);
for (auto off : offsets) ofs.write(reinterpret_cast<const char*>(&off), sizeof off);

Extra practical tips

  • Decide and document the offset base (file start or after header). A mismatch is the most common bug.
  • Handle endianness explicitly (define little- or big-endian format, or use network order functions) if the file will be read on different platforms.
  • Check write/seek return states and use 64-bit offsets if files may exceed 4 GB.
  • For debugging, inspect the binary with a hex dump (xxd/hexdump) and verify that the offset table entries point to the expected payload starts.

These changes make the format explicit, portable, and much easier to debug than trying to serialize raw pointer values.

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.