Currently migrating one of my program from Matlab
to C++
, I am experiencing a difficulty in reading a file.csv
and look for assistance for my understanding.
struct nav {
std::string title;
... // I have 17 members but for simplicity purposes I am only disclosing
// two of them
float quant;
};
nav port[];
std::string filedir = "C:\\local\\";
std::string fdbdir = filedir + "Factor\\";
std::string extension1 = "fdb.csv";
std::string extension2 = "nav.csv";
std::string factorpath = fdbdir + extension1;
std::string factorpath2 = filedir + extension2;
std::ifstream fdbdata(factorpath);
std::ifstream navdata(factorpath2);
int main() {
// 2nd data file involving data of different types
{
navdata.open(factorpath2);
if (navdata.fail()) {
std::cout << "Error:: nav data not found." << std::endl;
exit(-1);
}
for (int index = 0; index < 5; index++)
{
std::getline(navdata, port[index].title, ',');
std::getline(navdata, port[index].quant, ',');
}
for (int index = 0; index < 4; index++)
{
std::cout << port[index].title << " " << port[index].quant <<
std::endl;
}
}
}
Error: LNK2001: unresolved external symbol "struct nav * port" (?port@@3PAUnav@@A)
From the Error list
, there is certainly something wrong with the declaration of the struct type
port
that I'd like to know.
Most importantly: Is there a way of not hard-coding index
as the dimension of the data is not fixed. I've used for (int index = 0; index < 4; index++)
for testing purposes, but index
could be any integer as 50,200, etc.
EDIT:
As requested, please find below the minimal example:
struct Identity {
int ID;
std::string name;
std::string surname;
float grade;
};
std::string filedir = "C:\\local\\";
std::string extension = "sample.csv";
std::string samplepath = filedir + extension;
int main() {
std::ifstream test(samplepath);
std::vector<Identity> iden;
Identity i;
while (test >> i.ID >> i.name >> i.surname >> i.grade)
{
iden.push_back(i);
}
std::cout << iden[1].name;
system("pause");
}
resulting in vector subscript out of range
. Any idea of what looks wrong here?
Also the below sample data as requested: ps: the point header should be read grade for consistency purposes.
Best,