从文件写入类型为类的向量
Write to a Vector of Type Class from a File
将向量保存到文件中可以正常工作。但我正在寻找一种将保存的数据加载回矢量的简单方法。
这是我之前问过的两个后续问题。
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;
}
如果文件中
唯一写入Account
,您可以使用以下 1 行代码将它们全部读取到向量(或任何可push_back的容器)中:
std::copy(std::istream_iterator<Account>(file), std::istream_iterator<Account>(), std::back_inserter(vec));
您还需要一个类似于您已有operator<<
的operator>>
。
在这个问题中找到答案 使用用户定义的类类型对象的向量
对我来说,它是通过使用来解决的:
while(!inBankSysFile.eof())
{
Account a;
inBankSysFile >> a.accountID;
inBankSysFile >> a.accountPass;
inBankSysFile >> a.firstName;
inBankSysFile >> a.lastName;
inBankSysFile >> a.accountBalance;
accounts_.push_back(a);
}
如果你没有任何动态内存,你可以使用 ifstream::read 和 ofstream::write 和 vector::d ata 非常轻松地将其读写为二进制文件。 下面是一个示例:
#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)将需要额外的处理逻辑来处理该数据的读取和写入。
注意:使用任何对象的大小时,请注意结构对齐填充的差异。
相关文章:
- C++中函数的向量返回类型引发错误
- 具有 STL 向量类型成员的类的复制内存
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- 是否可以有一个模板函数,可以将向量和非向量类型都作为参数
- 具有基元类型的模板向量类型
- 在索引和值类型上参数化的惯用向量类型
- 使用迭代器成员函数是否仅适用于某些向量类型"empty()"?
- 通过glUniform 将 GLM 的向量类型传递给 OpenGL
- 尝试将用户定义的向量类型插入用户定义的类型向量
- 使用类作为向量类型时出错 - "Undeclared identifier"
- 在C++中,当两个向量类型不同时,如何将值存储到向量内部的向量中
- 如何访问和存储向量类型的结构成员
- 按参数中的向量类型重载函数
- boost::variant:具有递归向量类型的奇怪行为
- LLVM 传递计数向量类型指令
- 用异构boost::融合向量类型数据填充std::vector
- 如何定义自定义类的向量类型
- 真正的动态向量类型
- 如何在类的属性/变量中使用组合类作为向量类型
- 更快地删除向量类型