i am using VC6
Can Classes be written to a file.
And how
I have tried using fwrite but it didn't work
i am using VC6
Can Classes be written to a file.
And how
I have tried using fwrite but it didn't work
Last edited by fatta; 05-23-2002 at 02:04 AM.
Hi fatta,
You really have to be more specific on what exactly you want to do.
Regards,
Mario Figueiredo
Using Borland C++ Builder 5
Read the Tao of Programming
This advise was brought to you by the Comitee for a Service Packless World
Yes, you can. I haven't used VC, but that shouldn't be any difference from other compilers.
Code:MyClass MyObject; ... //(Set data) FILE* MyFile; MyFile=fopen("MyFile.sav", "wb") if(MyFile != NULL) { fwrite(&MyObject, sizeof(MyClass), 1, MyFile); fclose(MyFile); }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
If you do it that way and the class contains any dynamically allocated elements, then isn't it not going to work when you try to do a load because the addresses will be invalid?
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Magos thanks,
its works, no syntax errors but when i am reading back the value into another class, i am getting back garbage values.
if you overload the insertion operator (<<) you can save them to file just like you would print them to screen
>>isn't it not going to work when you try to do a load because the addresses will be invalid?
no, it will work because you are not saving the address, but the whole class with all info in binary mode to file. if you open it it will be jumbled garbage.. .. and you need to use the read() command to get stuff back am i not correct?
Hmm, I'm not sure but I think that you only save what is "in" the class object, ie Variables. Any data that is "outside" (dynamically allocated) will not be saved automatically, and needs to be saved separately.Originally posted by Unregistered
>>isn't it not going to work when you try to do a load because the addresses will be invalid?
no, it will work because you are not saving the address, but the whole class with all info in binary mode to file. if you open it it will be jumbled garbage.. .. and you need to use the read() command to get stuff back am i not correct?
[EDIT]
If you have pointers in the class, their values (what they are pointing at) will be saved too (I believe). When reading the class, these pointers will most likely point at random places in memory and could cause trouble if not handled properply.
[/EDIT]
Last edited by Magos; 05-23-2002 at 09:15 AM.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Hmm, how does your reading function look like?Originally posted by fatta
Magos thanks,
its works, no syntax errors but when i am reading back the value into another class, i am getting back garbage values.
Something like this should work:
Make sure you use the EXACT same class when writing and reading.Code:MyClass MyObject2; FILE* MyFile2; MyFile2 = fopen("MyFile.sav", "rb"); if(MyFile2 != NULL) { read(&MyObject2, sizeof(MyClass), 1, MyFile2); fclose(MyFile2); }
Also notice the "rb" insteaad of "wb" so you read the file instead of attempting to write to it again.
This might cause garbage values and other funny stuff to your files .
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
lets say you have this:
struct a
{
int * data;
}
and you write it to file like this:
fout << data;
what will happen? Your file will contain the address in data. But when you read the address back into memory from the file there may be something else stored in that memory location:
fin >> data;
cout << data; //will give a memory address
cout << *data; //will give garbage, or at best unreliable data.
Whenever you write data to file that involves pointers, whether static memory or dynamic memory, you should write the value of the data being pointed to, not it's address.
This:
fout << *data;
not this:
fout << data;