0

将矢量保存到文件中可以正常工作。但我正在寻找一种简单的方法将保存的数据加载回向量中。

这是我之前问过的两个问题的后续问题。

1) 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

If Accounts are the only thing written in your file you can read them all into your vector (or any push_back-able container) with this 1-liner:

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

You'll also need an operator>> analogous to the operator<< you already have.

于 2012-08-11T02:42:03.473 回答
0

在这个问题中找到了答案Using vector of user defined class type objects

对我来说,它是通过使用解决的:

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 回答
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 回答