943,516 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 859
  • C RSS
Nov 5th, 2008
0

Error in File write of dat file

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

Re: Error in File write of dat file

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

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: how to read data file
Next Thread in C Forum Timeline: warning C4715???





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


Follow us on Twitter


© 2011 DaniWeb® LLC