I have no problem in reading the contents of file , but I want to store the values in an array format. How can I do it ?
Contents of file:
1,2,4,4,0,30,15,7.6,5
1,2,4,5,0,0,0,0,0
1,3,2,1,0,40,29,14,9.6
1,3,2,2,0,0,19,9.4,6.2
Code:
ifstream infile;
infile.open ("test.txt");
if (infile.is_open())
{
while (infile.good())
{
cout << (char) infile.get();
}
infile.close();
}
else
{
cout << "Error opening file";
}
return 0;
void parser()
{
ifstream data("test.txt");
string line;
vector<vector<string>> parsedRow;
while(getline(data,line))
{
stringstream lineStream(line);
string cell;
vector<string> parsedRow;
while(getline(lineStream, cell, ','))
{
parsedRow.push_back(cell);
}
parsedCsv.push_back(parsedRow);
}
};
If you want float array,
void parser()
{
ifstream data("test.txt");
string line;
vector<vector<float>> parsedRow;
while(getline(data,line))
{
stringstream lineStream(line);
string cell;
vector<float> parsedRow;
while(getline(lineStream, cell, ','))
{
float f_cell = atof(cell.c_str());
parsedRow.push_back(f_cell);
}
parsedCsv.push_back(parsedRow);
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.