Solved

Write the class to a file

Posted on 2002-07-04
7
478 Views
Last Modified: 2012-08-14
Using MSVC++ 6.0

I created a class and populated it.  Let's call it myclass.  Once it gets populated I want to write this class to a file.  I opened the file using CFile and the type of file that I want to send it to is binary.  Lets call the opened file infile.  I used the following command to write the class to the file:

             infile.Read(myclass, sizeof(myclass));

I get the following error when I try to compile the program:

C:\ARDSAVEN\ARDSAVENDlg.cpp(348) : error C2440: 'type cast' : cannot convert from 'class adrasaven' to 'char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

ARDSAVEN.exe - 1 error(s), 0 warning(s)

What is the proper command that I may use to write the class to a file????

0
Comment
Question by:adatd
7 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7129889
If you class is a POD class, you can use the following:

infile.Read(&myclass, sizeof(myclass));
0
 

Author Comment

by:adatd
ID: 7129910
I tried that and I get a Debug Assertion Failed error, filecore.cpp when I execute the application.

0
 

Author Comment

by:adatd
ID: 7129922
CFile::Write(const void * 0x0064f4ac, unsigned int 0x0000003c) line 238 + 28 bytes
CARDSAVENDlg::OnProcessdata() line 350
_AfxDispatchCmdMsg(CCmdTarget * 0x0064fcec {CARDSAVENDlg}, unsigned int 0x000003e8, int 0x00000000, void (void)* 0x0040107d CARDSAVENDlg::OnProcessdata(void), void * 0x00000000, unsigned int 0x0000000c, AFX_CMDHANDLERINFO * 0x00000000) line 88
CCmdTarget::OnCmdMsg(unsigned int 0x000003e8, int 0x00000000, void * 0x00000000, AFX_CMDHANDLERINFO * 0x00000000) line 302 + 39 bytes
CDialog::OnCmdMsg(unsigned int 0x000003e8, int 0x00000000, void * 0x00000000, AFX_CMDHANDLERINFO * 0x00000000) line 97 + 24 bytes
CWnd::OnCommand(unsigned int 0x000003e8, long 0x00000610) line 2088
CWnd::OnWndMsg(unsigned int 0x00000111, unsigned int 0x000003e8, long 0x00000610, long * 0x0064f848) line 1597 + 28 bytes
CWnd::WindowProc(unsigned int 0x00000111, unsigned int 0x000003e8, long 0x00000610) line 1585 + 30 bytes
AfxCallWndProc(CWnd * 0x0064fcec {CARDSAVENDlg hWnd=???}, HWND__ * 0x00000614, unsigned int 0x00000111, unsigned int 0x000003e8, long 0x00000610) line 215 + 26 bytes
AfxWndProc(HWND__ * 0x00000614, unsigned int 0x00000111, unsigned int 0x000003e8, long 0x00000610) line 368
AfxWndProcBase(HWND__ * 0x00000614, unsigned int 0x00000111, unsigned int 0x000003e8, long 0x00000610) line 220 + 21 bytes
KERNEL32! bff63613()
KERNEL32! bff848f7()
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 32

Expert Comment

by:jhance
ID: 7130127
Do you have Visual Studio?

If so, a debugger is included.  Debug!
0
 
LVL 30

Expert Comment

by:Axter
ID: 7130144
Please post your class declaration.
0
 

Accepted Solution

by:
lcrogers earned 50 total points
ID: 7130315
I believe the way that you are doing it is on the right track.  the command is:

      myclass x;
      infile(&x, sizeof(class myclass));

Try it, it seems to work and you use the same for structures too!
0
 
LVL 30

Expert Comment

by:Axter
ID: 7132786
lcrogers,
I believe I made that suggestion in my first comment.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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