Solved

Write the class to a file

Posted on 2002-07-04
7
451 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maze travler 6 47
Compile GLUT with Visual Studio 2015 1 85
FMX enumerated colours 2 72
Unresolved External Symbols 3 53
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now