Page 1 of 1

Now here is a problem (with code plz help)

Posted: Wed Jan 11, 2006 7:56 am
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

Posted: Wed Jan 11, 2006 8:34 am
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

Posted: Wed Jan 11, 2006 11:03 am
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 ).

Posted: Wed Jan 11, 2006 12:33 pm
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.

Posted: Thu Jan 12, 2006 6:47 am
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.