Solved

Write the class to a file

Posted on 2002-07-04
7
494 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
Independent Software Vendors: 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!

 
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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 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