-1

I copy/paste this console application, which is a banking record keeping system, and I get an error for the ">" operator not having operands; if (infile.read(reinterpret_cast<char*>(this), sizeof(*this)) > 0)

Does it have something to do with types? I read vaguely somewhere about the int needing to be overloaded or something. Anyways, idk.

Here is part of the code:

void account_query::read_rec()
{
    ifstream infile;
    infile.open("record.bank", ios::binary);
    if (!infile)
    {
        cout << "Error in Opening! File Not Found!!" << endl;
        return;
    }
    cout << "\n****Data from file****" << endl;
    while (!infile.eof())
    {
        if (infile.read(reinterpret_cast<char*>(this), sizeof(*this)) > 0)
        {
            show_data();
        }
    }
    infile.close();
JimGordon
  • 375
  • 1
  • 7
  • `while (!infile.eof())` Read here please: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – πάντα ῥεῖ Nov 22 '20 at 10:57
  • 1
    Regarding your error, take a look ar `read()` return type in the docs please: https://en.cppreference.com/w/cpp/io/basic_istream/read – πάντα ῥεῖ Nov 22 '20 at 10:59

1 Answers1

1

ifstream::read() does not return the number of characters read. It returns a reference to itself.

Also, dont use while (!infile.eof()): Why is iostream::eof() inside a loop condition (i.e. while (!stream.eof())) considered wrong?

Instead, test if the ifstream is in a good state after reading from it. This can done with:

if(infile) { ... }

Since infile.read(...) returns a reference to infile, this could be a possible fix:

cout << "\n****Data from file****" << endl;
while (infile.read(reinterpret_cast<char*>(this), sizeof(*this)))
    show_data();
}
Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50