Answer the question
In order to leave comments, you need to log in
How to write data from a file to an array of structures?
Hello!
There is a file with lines of the form:
int1,string1,int2
There is a structure:
struct struct_name {
int int1;
string string1;
int int2;
};
vector<struct_name>vector_name
vector_name[0]
structure with variables from the first line of the file should be stored in. Answer the question
In order to leave comments, you need to log in
If string1 were represented as char string1[123], then struct_name would be a POD type. In this case, it could read like this:
vector<struct_name> vector_name;
FILE *fp = fopen(...);
size_t size = ...
vector_name.resize(size);
fread(&vector_name[0], sizeof(struct_name), size, fp);
struct struct_name {
int int1;
string string1;
int int2;
template<class Archive>
void save(Archive & ar) const
{
ar & int1;
ar & string1;
ar & int2;
}
template<class Archive>
void load(Archive & ar)
{
ar & int1;
ar & string1;
ar & int2;
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question