0

私はすでにいくつかのものをファイルに書き込んでいますが、今は内容を読んで画面に表示したいと思っています。表示する関数を書きましたが、何も表示されません。ビュー関数のコードは次のとおりです。2つの変数のみを表示するようにテストしていました。また、下部で呼び出される表示関数は、親クラスからのもので、他のクラスからのすべての変数を表示します

void ViewAll(string name, Intervention inte)
{
    ifstream clientfile(name, ios::in);
    if (clientfile)
    {
        int hour, min, day, month, yr, snum, age;
        string fname, lname, interNo, problem, clinic, area, ex,  li,                    type,      breed, gender, sname, town, pay;

        while (clientfile && !clientfile.eof())
        { //needed to loop through each record in the file
            clientfile >> interNo;
            clientfile >> clinic;
            clientfile >> lname;
            clientfile >> fname;
            clientfile >> pay;
            clientfile >> snum;
            clientfile >> sname;
            clientfile>> town;
            clientfile >> area;
            clientfile >> ex;
            clientfile >> li;
            clientfile >> type;
            clientfile >> breed;
            clientfile >> gender;
            clientfile >> problem;
            clientfile >> age;
            clientfile >> day;
            clientfile >> month;
            clientfile >> yr;
            clientfile >> hour;
            clientfile >> min;

            if (fname == inte.getClient().getFname())
            {
                break;
            }
        }

        //after record is found, create record
        inte.getClient();
        inte.display();
        system("pause");
    }

    //return inte;
}
4

2 に答える 2

0

inte のメンバーを読み込もうとしていますか? その場合、渡されたオブジェクトを変更できるように、参照によって inte を渡す必要があります。

clientfile >> inte.interNo;

作成したローカル変数はすべて役に立たないようです。

于 2013-11-03T02:53:02.087 に答える
0

出発点として、コードをかなり異なる構造にすることをお勧めします。それぞれのデータを読み書きするにはoperator>>、 とのオーバーロードから始めます。operator<<Intervention

std::istream &operator>>(std::istream &is, Intervention &i) { 
        is >> i.interNo;
        is >> i.clinic;
        is >> i.lname;
        is >> i.fname;

        // ...

        is >> i.min;
        return is;
}

...そしてそれに応じてoperator<<

std::ostream &operator>>(std::ostream &os, Intervention const &i) { 
        os << i.interNo;
        os << i.clinic;
        os << i.lname;
        os << i.fname;

        // ...

        os << i.min;
        return os;
}

これらを配置すると、 andをstd::copy使用する単純な呼び出しで、ファイルからすべてのレコードを表示できます。istream_iteratorostream_iterator

std::ifstream in(name);

std::copy(std::istream_iterator<Intervention>(in),
          std::istream_iterator<Intervention>(),
          std::ostream_iterator<Intervention>(std::cout, "\n"));

これにより、使用しようとするなど、コードに含まれるいくつかの問題が解消されます。

while (clientfile && !clientfile.eof())

コードのようなものwhile (!somefile.eof())は、ほぼ保証されたバグです (「ほぼ」は、このコードが正しく動作しない、または正しく動作しないという事実をカバーする他のコードを一緒に書くことができるためです)。

于 2013-11-03T04:43:06.700 に答える