CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Posts
    6

    Question plz help me with virtual functions file handling code

    here is the code

    plz help me with the case 2 present in main...

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

    class student
    {
    protected:

    int rollno;
    char name[50],degree;

    public:

    void virtual getdata()=0;
    void virtual putdata()=0;
    void virtual calresult()=0;
    };


    class acadmicublic student
    {
    protected:

    int imk1,imk2,emk1,emk2;
    float result;

    public:

    void getdata();
    void putdata();
    void calresult();
    };

    void acadmic::getdata()
    {
    cout<<"\n Enter Roll No:";
    cin>>rollno;
    cout<<"\n Enter Name:" ;
    cin>>name;
    cout<<"\n Enter Course:";
    cout<<"\n E for Enginnering \n M for MCA:";
    cin>>degree;
    cout<<"\n Enter Internal Marks for test1 and test 2:" ;
    cin>>imk1>>imk2;
    cout<<"\n Enter Marks For Extrenal Test1 and Test 2:";
    cin>>emk1>>emk2;

    }
    void acadmic:utdata()
    {
    cout<<"\n Roll No:"<<rollno;
    cout<<"\n Name:"<<name;
    cout<<"\n Course:"<<degree;
    cout<<"\n Internal Marks";
    cout<<"\n "<<imk1<<"\n "<<imk2;
    cout<<"\n External Marks";
    cout<<"\n "<<emk1<<"\n "<<emk2;
    cout<<"\n The Result Is "<<result;
    }
    void acadmic::calresult()
    {
    result=(imk1+imk2+emk1+emk2)/4;
    }

    void main()
    {
    student *ptr;
    acadmic a;
    ptr=&a;
    fstream file;

    file.open("stud.txt",ios::in|ios:ut);//|ios::binary);
    if(file==NULL)
    {
    cout<<"\nFile opening error....";
    getch();
    exit(1);
    }

    char ans;
    do
    {
    clrscr();
    cout<<"\n******** MENU ********\n\t1.Insert\n\t2.Display\n\t3.Search\n\t4.Modify\n\t5.Logical Delete\nEnter your choice?";

    switch(getche())
    {
    case '1':

    file.seekp(0,ios::end);
    ptr->getdata();
    ptr->calresult();
    file.write((char *)ptr,sizeof(a));

    file.clear();
    break;

    case '2': //DISPLAYING
    file.seekg(0,ios::beg);

    while(file)
    {

    file.read((char *)ptr,sizeof(a));
    ptr->putdata();//wat is the reason that it ddnt work
    //a.putdata(); //even this call works
    }
    //ptr->putdata(); //this works alone

    file.clear();
    break;

    }
    cout<<"\n Do you want to continue?";
    ans=getche();
    }while(ans=='y'||ans=='Y');
    file.close();
    cout<<"\nFile closed Successfully...";
    }



    thanks

    Nilesh

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: plz help me with virtual functions file handling code

    Please edit your post, and add code tags. This code is impossible to follow.

    Viggy

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: plz help me with virtual functions file handling code

    Quote Originally Posted by Philip Nicoletti View Post
    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).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 8th, 2010 at 01:51 PM.

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: plz help me with virtual functions file handling code

    Quote Originally Posted by fireblade2010 View Post
    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.

    On we go:
    Code:
    class student
    {
        protected:
    
         int rollno;
         char name[50],degree;
    public:
        void virtual getdata()=0;
        void virtual putdata()=0;
        void virtual calresult()=0;
    };
    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.

    Regards,

    Paul McKenzie

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: plz help me with virtual functions file handling code

    It's better not to cross post too so people don't waste time posting what others have already told you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured