Computer Science Canada

'read': identifier not found, short code plz help

Author:  chopficaro [ Tue Sep 02, 2008 11:22 am ]
Post subject:  'read': identifier not found, short code plz help

im just trying to get the hang of binary file io by making a simple program that can write a structure into a file, then read it, but the first error message i get is

'read': identifier not found

heres the code:

code:
#include <iostream>
#include <fstream>
using namespace std;

struct grades
{
int grade1;
int grade2;
int grade3;
int grade4;
} mygradesi,mygradeso;

int main ()
{
int answer;
do
{
cout<<"do u want to 1, write to the file or 2, read from the file, or 3, exit the program?";
cin>>answer;
if (answer==2)
{
struct grades * mygradesptri;
mygradesptri=&mygradesi;
ifstream myfilei;
myfilei.open ("binary.bin", ios::in|ios::binary);
if (myfilei.is_open())
{
read(mygradesptri, sizeof(grades));
cout<< mygradesi.grade1<< mygradesi.grade2<< mygradesi.grade3<< mygradesi.grade4;
}
else cout << "Unable to open file for input";
}
if (answer==1)
{
struct grades * mygradesptro;
mygradesptro=&mygradeso;
ofstream myfileo;
myfileo.open ("binary.bin", ios::out|ios::binary);
cout>>"please enter 4 grades";
cin<< mygradeso.grade1<< mygradeso.grade2<< mygradeso.grade3<< mygradeso.grade4;
if (myfileo.is_open())
{
write (mygradesptro, sizeof(grades));
myfileo.close();
}
else cout << "Unable to open file for output";
}
}while (answer!=3);
return 0;
}

Author:  chopficaro [ Tue Sep 02, 2008 11:46 am ]
Post subject:  RE:\'read\': identifier not found, short code plz help

how silly of me, i did not specify the name of the class. i ran into a new problem though, read and write are supposed to take char *, but im not trying to pass a char, im trying to pass a structure pointer, how do i do that?

error says cannot convert grades * to char *

code:

#include <iostream>
#include <fstream>
using namespace std;

struct grades
{
        int grade1;
        int grade2;
        int grade3;
        int grade4;
} mygradesi,mygradeso;

int main ()
{
        int answer;
        do
        {
                cout<<"do u want to 1, write to the file or 2, read from the file, or 3, exit the program?";
                cin>>answer;
                if (answer==2)
                {
                        char * mygradesptri;
                        mygradesptri=&mygradesi;
                        ifstream myfilei;
                        myfilei.open ("binary.bin", ios::in|ios::binary);
                        if (myfilei.is_open())
                        {
                                myfilei.read(mygradesptri, sizeof(grades));
                                cout<< mygradesi.grade1<< mygradesi.grade2<< mygradesi.grade3<< mygradesi.grade4;
                        }
                        else cout << "Unable to open file for input";
                }
                if (answer==1)
                {
                        char * mygradesptro;
                        mygradesptro=&mygradeso;
                        ofstream myfileo;
                        myfileo.open ("binary.bin", ios::out|ios::binary);
                        cout>>"please enter 4 grades";
                        cin<< mygradeso.grade1<< mygradeso.grade2<< mygradeso.grade3<< mygradeso.grade4;
                        if (myfileo.is_open())
                        {
                                myfileo.write (mygradesptro, sizeof(grades));
                                myfileo.close();
                        }
                        else cout << "Unable to open file for output";
                }
        }while (answer!=3);
        return 0;
}       

Author:  OneOffDriveByPoster [ Tue Sep 02, 2008 7:50 pm ]
Post subject:  Re: RE:\'read\': identifier not found, short code plz help

chopficaro @ Tue Sep 02, 2008 11:46 am wrote:
how silly of me, i did not specify the name of the class. i ran into a new problem though, read and write are supposed to take char *, but im not trying to pass a char, im trying to pass a structure pointer, how do i do that?
IIRC, you can cast the grades* to char* in the call to read() and it should work fine.

Author:  chopficaro [ Wed Sep 03, 2008 5:28 pm ]
Post subject:  RE:\'read\': identifier not found, short code plz help

brilliant! TY! works great!


: