plz help me with virtual functions file handling code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: plz help me with virtual functions file handling code

  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
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: plz help me with virtual functions file handling code

    Please use code tags for posting code.

    Code:
    file.read((char *)ptr,sizeof(a));
    You are casting a class pointer to a char pointer. What do you think will happen ? Also, sizeof(a). You are trying to take the size of a class. DON'T.

  3. #3
    Join Date
    Apr 2010
    Posts
    6

    Re: plz help me with virtual functions file handling code

    we always have to cast class pointer to char pointer while reading or writing to a file

    and also need to specify size of the object which i did

    plz tell me whats wrong with that function call

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: plz help me with virtual functions file handling code

    we always have to cast class pointer to char pointer while reading or writing to a file
    But what do you expect to happen ? You have a pointer to a class and you are telling the compiler that it is a character buffer. Do you expect to write the class to disk ? You can't write a class to disk. You can only write the data in the class members to disk (for example the 'name' and 'degree') but you need them to write them individually.

    So this..
    Code:
    file.write((char *)ptr,sizeof(a));
    and this...
    Code:
    file.read((char *)ptr,sizeof(a));
    is incorrect.

    This...
    Code:
    file.write(ptr->name,50);
    writes the name (50 chars) to disk.

  5. #5
    Join Date
    Apr 2010
    Posts
    6

    Re: plz help me with virtual functions file handling code

    but i want to write entire object
    and not the individual fields

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

    Re: plz help me with virtual functions file handling code

    You cannot do that. If it's a class, you must write each member individually. Philip has already said this in your other (duplicate) post:

    http://www.codeguru.com/forum/showthread.php?t=495486

    Viggy

  7. #7
    Join Date
    Apr 2010
    Posts
    6

    Re: plz help me with virtual functions file handling code

    ok
    i got what u r trying to explain

    now problem is since i am writing in file from main() hence i cant write each data member individually

    can u suggest any other approach

    thank you

  8. #8
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: plz help me with virtual functions file handling code

    now problem is since i am writing in file from main() hence i cant write each data member individually
    Why not ? What have the main function and writing to a file have to do with each other ? You can write to a file from every function you want.

  9. #9
    Join Date
    Apr 2010
    Posts
    6

    Re: plz help me with virtual functions file handling code

    thanks a lot sir

    i finally found a solution for my problem

    thanks a lot

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center