Ah yes I realized that with the missing #include <vector> afterwards. And have corrected the code to use push_back() for the vector. Thanks!
This is the new version. The only thing that isn't working is the following.
Code:
test.cpp:22: error: aggregate ‘std::stringstream strstream’ has incomplete type and cannot be defined
Also tried to use static_cast<int>(value) but didn't work. Any idea why? It'd just say the following.
Code:
test.cpp:46: error: invalid static_cast from type ‘std::string’ to type ‘int’
Code:
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
std::string name, age, sex;
std::string field, value;
std::string ifilename = "config.txt";
std::string lfilename = "out.log";
std::ifstream ifile(ifilename.c_str());
std::ofstream ofile(lfilename.c_str());
bool bname = false, bage = false, bsex = false;
int count = 0, valueInt;
std::stringstream strstream;
struct TypeStructConfig {
std::string name;
int age;
char sex;
} ;
typedef std::vector<TypeStructConfig> TypeVectorStructConfig;
TypeVectorStructConfig VectorStructConfig;
TypeStructConfig StructConfig;
/*
*
*/
int main(int argc, char** argv) {
while (getline(ifile, field, '=')) {
getline(ifile, value);
if (field == "NAME") {
StructConfig.name = value;
bname = true;
count++;
} else if (field == "AGE") {
//StructConfig.age = static_cast<int>(value);
strstream << value;
strstream >> valueInt;
StructConfig.age = valueInt;
bage = true;
count++;
} else if (field == "SEX") {
StructConfig.sex = value[0];
bsex = true;
count++;
} else {
ofile << field << " is an unknown configuration parameter.\n";
return 1;
}
if (count == 3 && (bname == false || bage == false || bsex == false)) {
ofile << "Not all required parameters are set.\n";
return 1;
} else {
VectorStructConfig.push_back(StructConfig);
count = 0;
}
}
return 0;
}