Solved

Write the class to a file

Posted on 2002-07-04
7
506 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 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