why doesn't this work?

These are the parts that might be useful to figure out why it is messing up. The error is in the load() function.

const int size=100;

struct inv_type {
char item[40];
double cost;
double retail;
int on_hand;
int lead_time;
} invtry[size];

void save()
{
bool status;

ofstream outfile;
status= writeFile(outfile, "inv.dat");
if (!status) {
cout << "File could not be opened for writing
";
}
else {
outfile << invtry;
outfile.close();
cout << "Saved.
";
}
}

void load()
{
bool status;

ifstream infile;
status= readFile(infile, "inv.dat");
if (!status) {
cout << "File could not be opened for reading
";
}
else {
infile >> invtry; //This is the error, illegal operand
infile.close();
cout << "Loaded
";
}
}

bool writeFile (ofstream& file, char* strFile)
{
file.open(strFile);
if (file.fail())
return false;
else return true;
}

bool readFile (ifstream& ifile, char* strFile)
{
ifile.open(strFile);
if (ifile.fail())
return false;
else return true;
}


Could you tell me why it isn't working

Comments

  • : These are the parts that might be useful to figure out why it is messing up. The error is in the load() function.
    :
    : const int size=100;
    :
    : struct inv_type {
    : char item[40];
    : double cost;
    : double retail;
    : int on_hand;
    : int lead_time;
    : } invtry[size];
    :
    : void save()
    : {
    : bool status;
    :
    : ofstream outfile;
    : status= writeFile(outfile, "inv.dat");
    : if (!status) {
    : cout << "File could not be opened for writing
    ";
    : }
    : else {
    : outfile << invtry;
    : outfile.close();
    : cout << "Saved.
    ";
    : }
    : }
    :
    : void load()
    : {
    : bool status;
    :
    : ifstream infile;
    : status= readFile(infile, "inv.dat");
    : if (!status) {
    : cout << "File could not be opened for reading
    ";
    : }
    : else {
    : infile >> invtry; //This is the error, illegal operand
    : infile.close();
    : cout << "Loaded
    ";
    : }
    : }
    :
    : bool writeFile (ofstream& file, char* strFile)
    : {
    : file.open(strFile);
    : if (file.fail())
    : return false;
    : else return true;
    : }
    :
    : bool readFile (ifstream& ifile, char* strFile)
    : {
    : ifile.open(strFile);
    : if (ifile.fail())
    : return false;
    : else return true;
    : }
    :
    :
    : Could you tell me why it isn't working
    :


    How to read the file depends on how it was written. Post the code that write the file. Is it a text file ? Are fields deliminated by something? You probably have to read the file just like you wrote it -- one field at a time.
  • : These are the parts that might be useful to figure out why it is messing up. The error is in the load() function.


    [blue]Use code tags when posting code:[/blue]

    [code]const int size=100;

    struct inv_type {
    char item[40];
    double cost;
    double retail;
    int on_hand;
    int lead_time;
    } invtry[ size ];

    void save()
    {
    bool status;

    ofstream outfile;
    status= writeFile(outfile, "inv.dat");
    if (!status) {
    cout << "File could not be opened for writing
    ";
    }
    else {
    [red]outfile << invtry;[/red]
    outfile.close();
    cout << "Saved.
    ";
    }
    }

    void load()
    {
    bool status;

    ifstream infile;
    status= readFile(infile, "inv.dat");
    if (!status) {
    cout << "File could not be opened for reading
    ";
    }
    else {
    [red]infile >> invtry;[/red] //This is the error, illegal operand
    infile.close();
    cout << "Loaded
    ";
    }
    }

    bool writeFile (ofstream& file, char* strFile)
    {
    file.open(strFile);
    if (file.fail())
    return false;
    else return true;
    }

    bool readFile (ifstream& ifile, char* strFile)
    {
    ifile.open(strFile);
    if (ifile.fail())
    return false;
    else return true;
    }[/code]

    [blue]The insertion (<<) and extraction (>>) stream operators are overloaded for all the basic data types, [italic]char[/italic], [italic]char*[/italic], [italic]int[/italic], [italic]float[/italic], [italic]double[/italic], etc... You however are attempting to use these operators on a custom type and the compiler has no idea what it means to insert or extract an [italic]inv_type struct[/italic]. You need to overload these operators youself for this to work as you want.

    Also, as used above, [italic]invtry[/italic] is treated as a pointer to type [italic]inv_type[/italic]. If you wanted to read and write a single element you would need to provide an index, i.e. [italic]infile >> invtry[0][/italic] to read a single record into the array at index 0. If you are aware of this and are attempting to write/read all records en masse using this pointer, I would suggest a loop instead.[/blue]

    :
    :
    : Could you tell me why it isn't working
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories