Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Write the class to a file

Posted on 2002-07-04
7
Medium Priority
?
529 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 150 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

886 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