If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: plz help me with virtual functions file handling code

It is undefined to read/write non-POD data in the manner that you are doing.
The easiest method would be to write the information in text mode instead
of unformatted. If you want to read unformatted, you need to do each
member individually.

Re: plz help me with virtual functions file handling code

Serialise your data by writing each individual member to a file, not the whole struct which is fraught with danger. To deserialise, read each member in from a file individually. Its fairly trivial to overload operators << and >> for your class.
Secondly you are using deprecated headers. The old iostream classes were deprecated many years ago. If your compiler is too old to support proper iostream use then get yourself a new compiler, theres a link for a couple in my sig. conio.h is nonstandard, so any facilities used from conio.h will need a revision. stdlib.h is a C header, the C++ version is <cstdlib>. main always returns an int never void.
I think you are probably using turbo C. This compiler is around 20 years old now and you cannot use it to learn C++ in any meaningful way.

Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors. Always use [code] code tags [/code] to make code legible and preserve indentation.Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Re: plz help me with virtual functions file handling code

Originally Posted by Philip Nicoletti

It is undefined to read/write non-POD data in the manner that you are doing.

It's amazing how so many posts show code doing this type of read/write of data to a file. A lot of times, you see a variation of this using "reinterpret_cast" and fwrite(). Are these posters reading from the same book or accessing the same web "tutorial"? If it's a book, I wonder what the name of it is?

I'm really curious, since it is obvious to any C++ programmer that has the knowledge to write a book or tutorial that this code will not work. It isn't even a matter of "it works on my compiler" -- it's just wrong. I'm shocked a book would show such code (not so shocked if it came from a web tutuorial).

Re: plz help me with virtual functions file handling code

Originally Posted by fireblade2010

here is the code

Let's go:

Code:

#include<iostream.h>
#include<fstream.h>

No. The correct headers are these:

Code:

#include<iostream>
#include<fstream>
#include<stdlib.h>

The <iostream> and <fstream> headers are the standard C++ headers, as defined by ANSI/ISO. The ".h" versions have been deprecated, and for some compilers, don't even exist. All C++ compilers that are ANSI standard must include the standard headers, so stick with standard headers.

As others pointed out, this class is non-POD, therefore you can't treat it as if it is a POD type. That's why you can't save it as you're doing to a file.

The term POD stands for Plain Old Data. A POD type is compatible (has the same layout) as a 'C' type, and this class is not 'C' layout compatible. The reason is that it has virtual functions, and multiple access specifiers (public and protected).

C++ has two basic types, POD and non-POD. For 'C', everything is POD, therefore my belief is that you're looking at 'C' code, and trying to make it C++ by just saving the class the same way. You cannot program that way if you're using C++, and that is to use 'C' or 'C'-like techniques in a C++ program in a haphazard manner.

The proper way to save the file, even for a C program, would be to write each member, one at a time to a file, so that you can recreate the class when you read the data from the file.

Code:

void main()

The main() function returns an int, not void.

You need to get C++ books, something that was written after 1998 (when C++ was standardized) that shows correct usage of the language. Second, if you have an old compiler, get rid of it. Get a compiler that follows the ANSI/ISO C++ standard. Compilers such as gcc or Visual Studio 2005 and above (the later version the better) are just two examples of compilers that are ANSI standard.

If you're using Turbo C++ (the one that came out in 1990 and last updated in 1994), you are wasting your time with that compiler. It is, as another poster pointed out, over 20 years old, is not ANSI standard.

There is a newer version of Turbo C++ that is supposedly ANSI standard, but I don't have enough info on it to recommend it.