Now here is a problem (with code plz help)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Morphing

Now here is a problem (with code plz help)

Post by Morphing »

class saving
{
f32 a1;
u32 a2;
const wchar_t* a3;

public:
saving()
{a1 = 2.0;a2 = 3;a3 = L"abc";}

void save()
{
a1 = 4;
a2 = 6;
a3 = L"abcde";
IWriteFile *fwrite = device->getFileSystem()->createAndWriteFile("aaa.ini", true);
fwrite->write(reinterpret_cast<char *>(this), sizeof(this));
fwrite->drop();
}

void load()
{
IReadFile *fast = device->getFileSystem()->createAndOpenFile("Optsdion.ini");
fast->read(reinterpret_cast<char *>(this), sizeof(this));
fast->drop();
}
void show()
{
std::cout<<a1<<"\n";
std::cout<<a2<<"\n";
std::wcout<<a3<<"\n";
}}s;


i called upon

s.save(); //for saving object
//s.load()
//s.show()

now when the file wz saved then i used

//s.save()
s.load();
s.show();

here the output screen showed me

4
3
abc

only first value is loaded correctly and the rest two , i don't know why they r not loaded.
r they saved or not plz help and if so why they r not on the output screen as the output should be 4 6 abcde
Guest

Post by Guest »

sorry i have miswritten in early post

IReadFile *fast = device->getFileSystem()->createAndOpenFile("aaa.ini");

now i changed the code a little bit and changed to

fwrite->write(reinterpret_cast<char *>(this), sizeof(saving));
fwrite->read(reinterpret_cast<char *>(this), sizeof(saving));
now it is showing first two variable correctly but it is not writing the wchar
srting. How can i write wchar string to the file
Warchief
Posts: 204
Joined: Tue Nov 22, 2005 10:58 am

Post by Warchief »

Havent tried, but think sizeof(this) doesnt include the length of a3, just the size of the pointer (that should be sizeof(int) ).

You should use something like sizeof(this) + length( a3 ).
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post by Baal Cadar »

That's right, Warchief. And furthermore only the pointer is written to the file, because technically only the wchar_t is member of the saving class. the actual string is somewhere in the data segment of this module.

There is no flexible way, to just put a variable length data block (like a string) into a file this way. Your save function has to take care of such data explicitly. For instance, if you write a string. First write the length of it, then read the next length characters into a newly created zero filled buffer of length+1 (0 terminated strings) and put the pointer to this buffer to your a3. Proceed accordingly to save it.

Code: Select all

// Made up from the top of my head, may or may not compile/work
 void save()
{
    a1 = 4; 
    a2 = 6;
    a3 = L"abcde";
    IWriteFile *fwrite = device->getFileSystem()->createAndWriteFile("aaa.ini", true);
    fwrite->write(&a1, sizeof(f32));
    fwrite->write(&a2, sizeof(u32));
    u16 count = strwlen(a3); // count of chars in the string. Needed for loading it.
    fwrite->write(&count, sizeof(u16));
    fwrite->write(a3, sizeof(wchar_t)*count);
    fwrite->drop();
}
Frankly, I'd not use the IRead/WriteFile interface for this. It is simple, but not very comfortable. std::iostream is much more comfortable, especially using filters, but one needs to get accustomed to it. Worth to take a look at it.
kushagra
Posts: 40
Joined: Sun Dec 04, 2005 10:18 am

Post by kushagra »

i think u should prefer XML parser for ur saving and loading which is i think more efficiant although binary is more faster but i prefer XML.
Post Reply