kind of aggravating

Thread Solved
Reply

Join Date: Jan 2008
Posts: 11
Reputation: loushou is an unknown quantity at this point 
Solved Threads: 0
loushou loushou is offline Offline
Newbie Poster

kind of aggravating

 
0
  #1
Jan 24th, 2008
I am trying to dump 2 longs, an array of floats and DWORDs, and an array of shorts into a file, USING BINARY out mode. Here is my code:
  1. #include <windows.h>
  2. #include <fstream>
  3. #include ".\vertexs.h"
  4.  
  5. using namespace std;
  6.  
  7. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  8. {
  9. OURCUSTOMVERTEX p_verts[5] =
  10. { (0.0f, 0.0f, 0.0f, 0xffffffff),
  11. (10.0f, 0.0f, 0.0f, 0xffffffff),
  12. (12.0f, 5.0f, 0.0f, 0xffffffff),
  13. (10.0f, 10.0f, 0.0f, 0xffffffff),
  14. (0.0f, 10.0f, 0.0f, 0xffffffff)
  15. };
  16. short p_Indices[9] =
  17. {
  18. 1, 2, 3, 1, 3, 5, 3, 4, 5
  19. };
  20.  
  21. long i_verts = 5;
  22. long i_inds = 9;
  23.  
  24. ofstream f_DataFile;
  25.  
  26. f_DataFile.open("chris.raw", ios_base::binary);
  27.  
  28. f_DataFile<<i_verts;
  29. f_DataFile<<i_inds;
  30.  
  31. for (int temp = 0; temp < i_verts; temp++)
  32. {
  33. f_DataFile<<p_verts[temp].x;
  34. f_DataFile<<p_verts[temp].y;
  35. f_DataFile<<p_verts[temp].z;
  36. f_DataFile<<p_verts[temp].color<<(int)0;
  37. }
  38.  
  39. for (int temp2 = 0; temp2 < i_inds; temp2++)
  40. {
  41. f_DataFile<<p_Indices[temp2];
  42. }
  43.  
  44. if (f_DataFile.fail())
  45. {
  46. return 1;
  47. }
  48.  
  49. return 0;
  50. }

vertexs.h just has the declaration of the struct OURCUSTOMVERTEX.

HERE is the crap that it is putting in my file (obviously not writing in binary out mode because this is just text).

  1. 594.29497e+0094.29497e+0094.29497e+009429496729504.29497e+0090000000000000000000123135345

I am looking for suggestions to solve my problem. maybe i missed something in my code... maybe it is all wrong.... maybe i am a retard.... or maybe this is a small issue that can easily be solved with another set of eyes....

thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: kind of aggravating

 
0
  #2
Jan 24th, 2008
> .. obviously not writing in binary out mode because this is just text
ios_base::binary only disables the control sequence translations ( for '\n', '\r' etc.) that take place during formatted input or output. to write raw binary data to a file, you will have to use unformatted output functions. eg. basic_ostream<>::write
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: kind of aggravating

 
0
  #3
Jan 24th, 2008
Yes, don't feel bad. This trips up a lot of people.

The << and >> are always for doing text output. If you know any C they are comparable to the printf() and scanf() functions. In other words, they always convert numbers to/from their string representations.

So, like vijayan121 said, you'll have to go to a raw output function:
f_DataFile.write( reinterpret_cast<const char *>( &i_verts ), sizeof( i_verts ) );

Good luck.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC