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*) classname, classname.GetLength());
Who is Participating?
AxterConnect With a Mentor Commented:
If your class is a POD object, then you can use the following method:*)classname, sizeof(classname));
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:

and here is an example that shows how easy it is:

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
If classname is a CString, you can use CStdioFile::ReadString to read it into the CString.

CStdioFile MyFile("data.txt");
CString MyCStr;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.