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

cpp Viet Nam
  • 9 years ago
    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;
    }    [/CODE]
  • 9 years ago

    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;
    }   
    [/CODE]

  • 9 years ago

    Hi ,

    Just cast to (char *), explicitly :

    myfilei.read( (char *)mygradesptri, sizeof(grades) );

  • 9 years ago

     ty! works perfectly! ur the best!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programs must be written for people to read, and only incidentally for machines to execute.”