0

ベクトルをファイルに保存すると正常に動作します。しかし、保存したデータをベクターにロードする簡単な方法を探しています。

これは、以前に尋ねた 2 つの質問へのフォローアップの質問です。

1) Private Vector へのデータ入力に関する C++ の問題 (無効な使用法)

2)型クラスのベクトルの出力

ファイルとpush_back()各要素を反復する簡単な方法は何ですか?

これはクラスです:

class Account
{
    private:
        string firstName;
        string lastName;
        string accountPass;
        int accountID;
        float accountBalance;

    public:
        static Account createAccount( int, float, string, string, string ); //creates new account
        int getAccountID() const { return accountID; }
        string getPass() const { return accountPass; }
        string getFirstName() const { return firstName; }
        string getLastName() const { return lastName; }
        float getBalance() const { return accountBalance; }
        friend std::ostream& operator << (std::ostream&, const Account&);
        friend class BankingSystem;

}; //end of class Account


Account Account::createAccount( int ID, float balance, string pass, string first, string last )
{    
    Account a;
    a.accountID = ID;
    a.accountPass = pass;
    a.firstName = first;
    a.lastName = last;
    a.accountBalance = balance;

    return a;

}

std::ostream & operator << (std::ostream & os, const Account & acc)
{
    os << setw(6) << acc.getAccountID();
    os << setw(4) << acc.getPass();
    os << setw(9) << acc.getFirstName();
    os << setw(9) << acc.getLastName();
    os << setw(9) << setprecision(2) << fixed << acc.getBalance();
    return os;
}
4

3 に答える 3

1

ファイルに書き込まれるのがsだけの場合Accountは、次の1ライナーを使用して、それらすべてをベクター(またはpush_back可能なコンテナー)に読み込むことができます。

std::copy(std::istream_iterator<Account>(file), std::istream_iterator<Account>(), std::back_inserter(vec));

また、すでに持っているものoperator>>に類似したものが必要になります。operator<<

于 2012-08-11T02:42:03.473 に答える
0

動的メモリがない場合は、ifstream::read と ofstream::write と vector::data を使用して、非常に簡単に読み書きできます。次に例を示します。

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

class Time
{
public:
Time(): hh(0),mm(0),ss(0) {}
Time(int h,int m,int s):hh(h),mm(m),ss(s) {}
int hh,mm,ss;
};

int main()
{
    Time time1(11,22,33);
    Time time2(44,55,66);

    vector<Time> timeList;
    timeList.push_back(time1);
    timeList.push_back(time2);

    vector<Time> timeList2;
    timeList2.resize(2,Time());

    ofstream fout;
    fout.open("test.txt");
    if(fout.is_open())
    {
        // vector.data returns a pointer to the beginning of its stored data
        // 1st param: the location to read data from
        // 2nd param: the amount of bytes to write to the file
        fout.write((const char*)timeList.data(),sizeof(Time)*timeList.size());

        fout.close();
    }

    ifstream fin;
    fin.open("test.txt");
    if(fin.is_open())
    {
        // 1st param: the location to write data to
        // 2nd param: the amount of bytes to read from the file
        // NOTE: make sure you've sized the vector appropriately before writing to it.
        fin.read((char*)timeList2.data(),sizeof(Time)*timeList2.size());

        for(int i=0;i<timeList2.size();++i) {
            cout << timeList2[i].hh << ":" << timeList2[i].mm << ":" << timeList2[i].ss << "\n";
        }

        fin.close();
    }


    return 0;
}

注: 動的メモリを使用するオブジェクト (std::string などの動的メモリを含むクラスを含むオブジェクトを含む) の読み取り/書き込みには、そのデータの読み取りと書き込みを処理する追加の処理ロジックが必要になります。

注: オブジェクトのサイズを使用する場合は、構造的なアラインメント パディングの差異に注意してください。

于 2012-08-11T04:48:29.120 に答える
0

ユーザー定義のクラス型オブジェクトのベクトルを使用して、この質問で答えを見つけました

私にとっては、次を使用して解決されました:

while(!inBankSysFile.eof()) 
{
    Account a;
    inBankSysFile >> a.accountID;
    inBankSysFile >> a.accountPass;
    inBankSysFile >> a.firstName;
    inBankSysFile >> a.lastName;
    inBankSysFile >> a.accountBalance;
    accounts_.push_back(a);

}
于 2012-08-11T03:05:31.860 に答える