The text file provided has an undetermined number of lines, each line containing 3 doubles separated by commas. For example:
-0.30895,0.35076,-0.88403
-0.38774,0.36936,-0.84453
-0.44076,0.34096,-0.83035
...
I want to read this data from the file line by line and then split it on the comma(,) sign and save it in an N by 3 array, let's call it Vertices [N] [3], where N designates the undefined number of lines in the file.
My code so far:
void display() {
string line;
ifstream myfile ("File.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
// I think the I should do 2 for loops here to fill the array as expected
}
myfile.close();
}
else cout << "Unable to open file";
}
The Problem: I managed to open the file and read line by line, but I have no idea how to pass the values into the requested array. Thank you.
EDIT: I have tried modifying my code according to the suggestions i received to the following:
void display() {
string line;
ifstream classFile ("File.txt");
vector<string> classData;
if (classFile.is_open())
{
std::string line;
while(std::getline(classFile, line)) {
std::istringstream s(line);
std::string field;
while (getline(s, field,',')) {
classData.push_back(line);
}
}
classFile.close();
}
else cout << "Unable to open file";
}
Is this the correct? and how can i access each field of the vector i created? (like in an array for example)? I also noticed that these are of type string, how can i convert them to type float? Thank you (: