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:

#include <windows.h>
#include <fstream>
#include ".\vertexs.h"

using namespace std;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	OURCUSTOMVERTEX p_verts[5] =
	{	(0.0f, 0.0f, 0.0f, 0xffffffff),
		(10.0f, 0.0f, 0.0f, 0xffffffff),
		(12.0f, 5.0f, 0.0f, 0xffffffff),
		(10.0f, 10.0f, 0.0f, 0xffffffff),
		(0.0f, 10.0f, 0.0f, 0xffffffff)
	};
	short p_Indices[9] =
	{
		1, 2, 3, 1, 3, 5, 3, 4, 5
	};

	long i_verts = 5;
	long i_inds = 9;

	ofstream f_DataFile;
	
	f_DataFile.open("chris.raw", ios_base::binary);

	f_DataFile<<i_verts;
	f_DataFile<<i_inds;

	for (int temp = 0; temp < i_verts; temp++)
	{
		f_DataFile<<p_verts[temp].x;
		f_DataFile<<p_verts[temp].y;
		f_DataFile<<p_verts[temp].z;
		f_DataFile<<p_verts[temp].color<<(int)0;
	}

	for (int temp2 = 0; temp2 < i_inds; temp2++)
	{
		f_DataFile<<p_Indices[temp2];
	}

	if (f_DataFile.fail())
	{
		return 1;
	}

	return 0;
}

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).

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

Recommended Answers

All 2 Replies

> .. 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

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.

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.