Error in File write of dat file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

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

Error in File write of dat file

 
0
  #1
Nov 5th, 2008
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




  1. char *constDataArea;
  2. const size_t BUFSIZE = 1024*1024;
  3. constDataArea=(char *)malloc(BUFSIZE);
  4. size_t datasize=0, nextpos=0, freepos=0;
  5. for(int nv_data=0,indx=0;nv_data<nonVoltleData;nv_data++)
  6. {
  7. switch(d_type)
  8. {
  9. case POINT_BOOL:
  10. data.Bool=(char)tmpbuf[index+2+nv_data+indx];
  11. fprintf(out_fp,"\nBool Data1:%x\n", data.Bool);
  12. datasize=1;
  13.  
  14. nextpos=freepos+datasize;
  15. memcpy(constDataArea+freepos,(char *)&data.Bool, datasize); /*data.bool から ConstDataArea にコピー*/
  16. freepos=nextpos;
  17. break;
  18. case POINT_CHAR:
  19. data.Char=(char)tmpbuf[index+2+nv_data+indx];
  20. fprintf(out_fp,"\nChar Data1:%x\n", data.Char);
  21. datasize=sizeof(data.Char);
  22.  
  23. nextpos=freepos+datasize;
  24. memcpy(constDataArea+freepos,(char *)&data.Char, datasize); /*data.Char から ConstDataArea にコピー*/
  25. freepos=nextpos;
  26. break;
  27. case POINT_SHORT:
  28. data.Short=(short)tmpbuf[index+2+nv_data+indx];
  29. fprintf(out_fp,"\nShort Data1:%x\n", data.Short);
  30. datasize=sizeof(data.Short);
  31.  
  32. if(freepos % sizeof(data.Short))
  33. {
  34. freepos+= sizeof(data.Short)-freepos % sizeof(data.Short);
  35. }
  36. nextpos = freepos + datasize;
  37. fprintf(out_fp,"S:ConstArea:%d\tfreepos:%d\n", constDataArea, freepos);
  38. memcpy(constDataArea+freepos,(char *)&data.Short,datasize); /*data.Short から ConstDataArea にコピー*/
  39. freepos=nextpos;
  40. break;
  41. case POINT_LONG:
  42. data.Long=(long)tmpbuf[index+2+nv_data+indx];
  43. fprintf(out_fp,"\nLong Data1:%x\n", data.Long);
  44. datasize=sizeof(data.Long);
  45.  
  46. if(freepos % sizeof(data.Long))
  47. {
  48. freepos+= sizeof(data.Long)-freepos % sizeof(data.Long);
  49. }
  50. nextpos=freepos+datasize;
  51. memcpy(constDataArea+freepos,(char *)&data.Long, datasize); /*data.Long から ConstDataArea にコピー*/
  52. freepos=nextpos;
  53. break;
  54. case POINT_FLOAT:
  55. data.Float=*(float*)&tmpbuf[index+2+nv_data+indx];
  56. fprintf(out_fp,"\nFloat Data1:%f\n", data.Float);
  57. datasize=sizeof(data.Float);
  58.  
  59. if(freepos % sizeof(data.Float))
  60. {
  61. freepos+= sizeof(data.Float)-freepos % sizeof(data.Float);
  62. }
  63. nextpos=freepos+datasize;
  64. memcpy(constDataArea+freepos,(char *)&data.Float, datasize); /*data.Float から ConstDataArea にコピー*/
  65. freepos=nextpos;
  66. break;
  67. case POINT_DOUBLE:
  68. data.Double = *(double*)&tmpbuf[index+2+nv_data+indx];
  69. fprintf(out_fp,"\nDouble Data1:%lf(0x%0x)\n", data.Double, data.Double);
  70. datasize=sizeof(data.Double);
  71.  
  72. if(freepos % sizeof(data.Double))
  73. {
  74. freepos+= sizeof(data.Double)-freepos % sizeof(data.Double);
  75. }
  76. nextpos=freepos+datasize;
  77. memcpy(constDataArea+freepos,(char *)&data.Double, datasize); /*data.Double から ConstDataArea にコピー*/
  78. freepos=nextpos;
  79. break;
  80. }
  81. }
  82. FILE *f1;
  83. f1=fopen("c:\\ambi.dat", "w");
  84. fwrite(constDataArea, 1,4, f1);
  85. fclose(f1);
  86.  
  87. printf("\nconst area:0x%x\t0x%x\t0x%x\t0x%x\n\n",constDataArea[0],constDataArea[1],constDataArea[2], constDataArea[3]);
Last edited by ambarisha.kn; Nov 5th, 2008 at 12:59 am.
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: Error in File write of dat file

 
0
  #2
Nov 5th, 2008
Its so simple, we have to write "wb" in fwrite instead of "w" , while writing binary file.
  1. FILE *f1;
  2. f1=fopen("c:\\ambi.dat", "wb");
  3. fwrite(constDataArea, 1,4, f1);
  4. fclose(f1);


I did changes as above and i got output..
Last edited by ambarisha.kn; Nov 5th, 2008 at 10:54 pm.
Ambarish
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC