Link to home
Start Free TrialLog in
Avatar of adatd
adatd

asked on

Read file data into a class

How can I read the data from a file straight into a MFC class using visual c++ 6.0?  I know that you can do it using a structure but is there a way to do it using a class?

  I tried using infile.read((char*) classname, classname.GetLength());
Avatar of DanRollins
DanRollins
Flag of United States of America image

MFC supports a concept called "object serialization"  All objects that are derived from CObject have this capability.  You just override the Serialize member function and then send the data to disk or read it from disk.

Here is a starting point for you:
http://search.microsoft.com/gomsuri.asp?n=1&c=rp_Results&siteid=us/dev&target=http://msdn.microsoft.com/library/en-us/vccore98/HTML/_core_serialization_.28.object_persistence.29.asp

and here is an example that shows how easy it is:
http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_cobject.3a3a.serialize.asp

-=-==-=--==-
It is also possible to just dump a blob of data into the area where the object keeps its data members (much as it sounds like you might be doing with a struct).  But it is never recommended because of the danger of overwriting important areas of memory.

-- Dan
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If classname is a CString, you can use CStdioFile::ReadString to read it into the CString.

Example:
CStdioFile MyFile("data.txt");
CString MyCStr;
MyFile.ReadString(MyCStr);