Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 'read': identifier not found, short code plz help
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chopficaro




PostPosted: 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;
}
Sponsor
Sponsor
Sponsor
sponsor
chopficaro




PostPosted: 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;
}       
OneOffDriveByPoster




PostPosted: 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.
chopficaro




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

brilliant! TY! works great!
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: