Trying to make some bin files

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Mark_Solo
Posts: 10
Joined: Sun Sep 25, 2005 6:30 pm

Trying to make some bin files

Post by Mark_Solo »

Well, i've been working on a way to keep some profiles on a binary file

the code is this

Code: Select all

#include <irrlicht.h>
#pragma comment(lib,"irrlicht.lib")
#include <iostream>
#include <fstream>

#define IRRCHAR const unsigned short

struct JPerfil {

		 wchar_t alias[80];
		 wchar_t nombrep[80]; 
};

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
//using namespace System;


void Escribir(const char *  nombre)
{

	JPerfil me;
	//me.alias = L"Mark Solo";
	//me.nombrep = L"Marco";
	wprintf (me.alias, L"Mark Solo");
	wprintf (me.nombrep, L"Marco");


	ofstream data_file;
	data_file.open(nombre,  ios::binary | ios::out | ios::trunc );
	data_file.seekp(0,ios_base::beg);
	data_file.write((const char *) &me, sizeof me); 
	data_file.close();
	cout << "Archivo escrito correctamente" << endl;
	
}




int main()
{
	const char* filename = "prueba.plb";
	int respuesta;
	cout << "Escribiendo archivos de perfil de HZII" << endl;

	Escribir(filename);
	double il,pl;
	JPerfil* m_me = new JPerfil[10];
	ifstream i(filename, ios::binary); //i.open("prueba.plb", ios::binary );
	
	il = i.tellg();
	i.seekg(0, ios::end);
	pl = i.tellg();
	i.read((char*)m_me, sizeof (JPerfil) * 10);
	i.close();
	cout << "Archivo Leido exitosamente: " << filename << " pesando "<< (pl-il) << " bytes."<< endl;


	using namespace irr;
	using namespace core;
	
    IrrlichtDevice *device = createDevice(video::EDT_DIRECT3D8,
            core::dimension2d<s32>(640,480));

    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* scenemgr = device->getSceneManager();
	gui::IGUIEnvironment* gui = device->getGUIEnvironment();

    device->setWindowCaption(m_me->nombrep);
	std::cout << "Nombre del jugador: "<< m_me->nombrep << std::endl;
	gui->addStaticText(m_me->alias,core::rect<s32>(40,20,200,60),true);
  
    while(device->run() && driver)
    {
            driver->beginScene(true, true, video::SColor(255,0,0,255));
            scenemgr->drawAll();
			gui->drawAll();
            driver->endScene();
    }

     
    device->drop();
  

	return 0;
}


the thing is that when i need to get the string values, iget some weird number, i think is the memory address alocated, and not the string..

please help :x
Warchief
Posts: 204
Joined: Tue Nov 22, 2005 10:58 am

Post by Warchief »

Uhm

1) use wsprintf instead of wprintf.
http://www.maconlinux.net/linux-man-pag ... ntf.3.html


2) If need to write the strings to the console, use std::wcout instead of std::cout.

3) Remeber that you are seeking to end before reading:

Code: Select all

   i.seekg(0, ios::end);
   pl = i.tellg();
   i.read((char*)&m_me, sizeof (JPerfil));
So you aren't reading anything. Remeber to reset the file pointer i.seekg(0, ios::beg);

4) Beware you are using a 10 profiles array instead of one structure.

5) Storing 320bytes for each profile is not optimal for disk space; take care about it
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

And wchar_t is not guaranteed to be two bytes on all platforms, so as always with binary representations, expect some problems with platform independence.
Mark_Solo
Posts: 10
Joined: Sun Sep 25, 2005 6:30 pm

Post by Mark_Solo »

Warchief wrote:Uhm

1) use wsprintf instead of wprintf.
http://www.maconlinux.net/linux-man-pag ... ntf.3.html


2) If need to write the strings to the console, use std::wcout instead of std::cout.

3) Remeber that you are seeking to end before reading:

Code: Select all

   i.seekg(0, ios::end);
   pl = i.tellg();
   i.read((char*)&m_me, sizeof (JPerfil));
So you aren't reading anything. Remeber to reset the file pointer i.seekg(0, ios::beg);

4) Beware you are using a 10 profiles array instead of one structure.

5) Storing 320bytes for each profile is not optimal for disk space; take care about it

OMAG Man i <3 you :D

no seriusly, you helped me to get the program working right so thanks a lot :D

PS: it wasn't wsprintf but swprintf
Warchief
Posts: 204
Joined: Tue Nov 22, 2005 10:58 am

Post by Warchief »

So nice.
Post Reply