Consider d_type=POINT_LONG in the following program.
and data.Long=0xA;

In the following program i am getting correct output in constDataArea[0],constDataArea[1],constDataArea[2], constDataArea[3]
which is oxA, 0x0, 0x0, 0x0.

But not able to write the same into ambi.dat file. Some junk value is inserting in the front.
and writing 5 bytes instead of 4 bytes.
Output file(ambi.dat)is as follows
-----------------
0D 0A 00 00 00
-----------------

Expected output in dat file is
---------------
0A 00 00 00
--------------
What is the error in this code. plz give me suggestion

char *constDataArea;
const size_t BUFSIZE = 1024*1024;	
constDataArea=(char *)malloc(BUFSIZE);
size_t datasize=0, nextpos=0, freepos=0;
for(int nv_data=0,indx=0;nv_data<nonVoltleData;nv_data++)
{
switch(d_type)
{
	case POINT_BOOL:
			data.Bool=(char)tmpbuf[index+2+nv_data+indx];				
			fprintf(out_fp,"\nBool Data1:%x\n", data.Bool);
			datasize=1;
			
			nextpos=freepos+datasize;
			memcpy(constDataArea+freepos,(char *)&data.Bool, datasize);   /*data.bool から ConstDataArea にコピー*/
			freepos=nextpos;
			break;
	case POINT_CHAR:
			data.Char=(char)tmpbuf[index+2+nv_data+indx];
			fprintf(out_fp,"\nChar Data1:%x\n", data.Char);
			datasize=sizeof(data.Char);
			
			nextpos=freepos+datasize;
			memcpy(constDataArea+freepos,(char *)&data.Char, datasize);		/*data.Char から ConstDataArea にコピー*/
			freepos=nextpos;
			break;
	case POINT_SHORT:
			data.Short=(short)tmpbuf[index+2+nv_data+indx];
			fprintf(out_fp,"\nShort Data1:%x\n", data.Short);
			datasize=sizeof(data.Short);
			
			if(freepos % sizeof(data.Short))
			{
				freepos+= sizeof(data.Short)-freepos % sizeof(data.Short);
			}
			nextpos = freepos + datasize;
			fprintf(out_fp,"S:ConstArea:%d\tfreepos:%d\n", constDataArea, freepos);
			memcpy(constDataArea+freepos,(char *)&data.Short,datasize);		/*data.Short から ConstDataArea にコピー*/
			freepos=nextpos;
			break;
	case POINT_LONG:
			data.Long=(long)tmpbuf[index+2+nv_data+indx];
			fprintf(out_fp,"\nLong Data1:%x\n", data.Long);
			datasize=sizeof(data.Long);
			
			if(freepos % sizeof(data.Long))
			{
				freepos+= sizeof(data.Long)-freepos % sizeof(data.Long);
			}
			nextpos=freepos+datasize;
			memcpy(constDataArea+freepos,(char *)&data.Long, datasize);		/*data.Long から ConstDataArea にコピー*/
			freepos=nextpos;
			break;
	case POINT_FLOAT:
			data.Float=*(float*)&tmpbuf[index+2+nv_data+indx];
			fprintf(out_fp,"\nFloat Data1:%f\n", data.Float);
			datasize=sizeof(data.Float);
			
			if(freepos % sizeof(data.Float))
			{
				freepos+= sizeof(data.Float)-freepos % sizeof(data.Float);
			}
			nextpos=freepos+datasize;
			memcpy(constDataArea+freepos,(char *)&data.Float, datasize);	/*data.Float から ConstDataArea にコピー*/
			freepos=nextpos;
			break;
	case POINT_DOUBLE:
			data.Double = *(double*)&tmpbuf[index+2+nv_data+indx];
			fprintf(out_fp,"\nDouble Data1:%lf(0x%0x)\n", data.Double, data.Double);
			datasize=sizeof(data.Double);
		
			if(freepos % sizeof(data.Double))
			{
				freepos+= sizeof(data.Double)-freepos % sizeof(data.Double);
			}	
			nextpos=freepos+datasize;
			memcpy(constDataArea+freepos,(char *)&data.Double, datasize);	/*data.Double から ConstDataArea にコピー*/
			freepos=nextpos;
			break;										
	}
}
FILE *f1;
f1=fopen("c:\\ambi.dat", "w");
fwrite(constDataArea, 1,4, f1); 	
fclose(f1);

printf("\nconst area:0x%x\t0x%x\t0x%x\t0x%x\n\n",constDataArea[0],constDataArea[1],constDataArea[2], constDataArea[3]);

Its so simple, we have to write "wb" in fwrite instead of "w" , while writing binary file.

FILE *f1;
f1=fopen("c:\\ambi.dat", "wb");
fwrite(constDataArea, 1,4, f1); 	
fclose(f1);

I did changes as above and i got output..

This question has already been answered. Start a new discussion instead.