Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read file data into a class

Posted on 2002-07-02
3
Medium Priority
?
186 Views
Last Modified: 2010-04-01
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());
0
Comment
Question by:adatd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 7125571
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
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 7125751
If your class is a POD object, then you can use the following method:
infile.read((char*)classname, sizeof(classname));
0
 
LVL 30

Expert Comment

by:Axter
ID: 7125754
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);
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question