我已成功按照here发布的答案将结构(类型image_info_t
)写入文件。
我在N
个image_info_t
个循环中重复该过程,所有数据都被序列化并正确添加到文件中。
我现在需要读取该文件,但我需要能够读取任意数字M
,image_info_t
结构以从文件中读取(按顺序排列)。上面引用的答案明确地硬编码从文件中回读的结构的数量(即student_t master[3];
)。但是,我需要这个数字是动态的。
我已阅读here “C ++标准要求数组在声明其大小时使用整数文字或整数常量。使用<vector>
代替”
我的问题是:我怎么能这样做? 如何将image_info_t
结构从文件中读回std::vector
?
这是我用来从文件中读取image_info_t
数据的当前(非工作)代码。
std::ifstream input_file(path, std::ios::binary);
const int kpts_size = kpts.size();
feature_t master[kpts_size]; //DOES NOT WORK. If I change to `feature_t master[10];` it works.
input_file.read((char*)&master, sizeof(master));
input_file.close();
注意:这不是访问冲突问题,与“可能的重复”答案无关。当你这样标记它时,人们就会停止阅读我的问题,这对任何人都没有帮助。
答案 0 :(得分:1)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
//Some kind of structure containing many sample data types..
typedef struct image_info
{
char image_type;
std::uint32_t md5_hash;
std::string image_name;
std::vector<std::uint8_t> bytes;
} image_info_t;
//Used for writing the above structure to a stream..
std::ostream& operator << (std::ostream& os, const image_info &entry)
{
std::size_t image_name_size = entry.image_name.size();
std::size_t image_bytes_size = entry.bytes.size();
os.write(&entry.image_type, sizeof(entry.image_type));
os.write(reinterpret_cast<const char*>(&entry.md5_hash), sizeof(entry.md5_hash));
os.write(reinterpret_cast<const char*>(&image_name_size), sizeof(image_name_size));
os.write(entry.image_name.c_str(), entry.image_name.size());
os.write(reinterpret_cast<const char*>(&image_bytes_size), sizeof(image_bytes_size));
os.write(reinterpret_cast<const char*>(&entry.bytes[0]), entry.bytes.size());
return os;
}
//Used for reading the above structure from a stream..
std::istream& operator >> (std::istream& is, image_info &entry)
{
std::size_t image_name_size = 0;
std::size_t image_bytes_size = 0;
is.read(&entry.image_type, sizeof(entry.image_type));
is.read(reinterpret_cast<char*>(&entry.md5_hash), sizeof(entry.md5_hash));
is.read(reinterpret_cast<char*>(&image_name_size), sizeof(image_name_size));
entry.image_name.resize(image_name_size);
is.read(&entry.image_name[0], image_name_size);
is.read(reinterpret_cast<char*>(&image_bytes_size), sizeof(image_bytes_size));
entry.bytes.resize(image_bytes_size);
is.read(reinterpret_cast<char*>(&entry.bytes[0]), image_bytes_size);
return is;
}
//Used for writing an array/vector of the above structure to a stream..
std::ostream& operator << (std::ostream& os, const std::vector<image_info> &entry)
{
std::size_t entry_size = entry.size();
os.write(reinterpret_cast<const char*>(&entry_size), sizeof(entry_size));
for (std::size_t i = 0; i < entry_size; ++i)
os << entry[i];
return os;
}
//Used for reading an array/vector of the above structure from a stream..
std::istream& operator >> (std::istream& is, std::vector<image_info> &entry)
{
std::size_t entry_size = 0;
is.read(reinterpret_cast<char*>(&entry_size), sizeof(entry_size));
entry.resize(entry_size);
for (std::size_t i = 0; i < entry_size; ++i)
is >> entry[i];
return is;
}
int main()
{
std::vector<image_info_t> outdata;
std::vector<image_info_t> indata;
image_info_t one;
image_info_t two;
one.image_name = "one";
one.image_type = 'a';
one.md5_hash = 1;
one.bytes.push_back(0);
two.image_name = "two";
two.image_type = 'b';
two.md5_hash = 2;
two.bytes.push_back(1);
outdata.push_back(one);
outdata.push_back(two);
std::fstream out("C:/Users/School/Desktop/Image_Info_T.bin", std::ios::out | std::ios::binary);
if (out.is_open())
{
out << outdata;
out.close();
}
std::fstream in("C:/Users/School/Desktop/Image_Info_T.bin", std::ios::in | std::ios::binary);
if (in.is_open())
{
in >> indata;
}
std::cout<<indata[0].image_name<<" "<<indata[1].image_name;
}
答案 1 :(得分:0)
如果您想避免使用矢量,可以通过执行以下操作来初始化数组:
feature_t* master = new feature_t[kpts.size()];
//code
delete[] master;
或者,使用向量,您只需创建feature_t的向量,IE:
std::vector<feature_t> master;
通常我发现向向量添加结构或类的最简单方法是创建它们的实例,然后填充所有值并将其添加到向量中,所以我可能会这样做:
feature_t temp;
while (getline(file, str))
{
temp.a = ...;
temp.b = ...;
master.push_back(temp);
}
在C中,new
将替换为malloc
(或其衍生函数之一),因此您将使用:
feature_t* master = malloc(sizeof(master) * kpts.size());
//code
free(master);