Hello, so I am trying to read data from a file, each line has 7 pieces of information i need to store in different variables. Im not sure how to separate pieces of each line. I could really use some help
int main(int argc, constchar * argv[]) {
string baseDir = "C:\\Users\\Brian\\Desktop/";
Triangle triangles[100];
int tCount = 0;
constint BUFFER_SIZE = 128; //
char buffer[BUFFER_SIZE];
string oneTriangleData[10]; // There are 7 data points for each triangle
ifstream datafile;
datafile.open(baseDir + "triangles6.csv");
while (!datafile.eof()) {
datafile >> buffer;
cout << buffer << endl;
splitStringByCommas(buffer, oneTriangleData);
// Each line in the file is one triangle
// The lines are encoded like this:
// x1,y1,x2,y2,x3,y3,color
// with commas between the data points, and no spaces
}
datafile.close();
// This function was provided by the teacher
void splitStringByCommas(string s, string pieces[]) {
size_t comma = 0;
int piece = 0;
while (comma != string::npos) {
comma = s.find(',');
pieces[piece++] = s.substr(0, comma);
s = s.substr(comma + 1);
}
pieces[piece] = s; // remainder
}
struct Triangle
{
int x[3];
int y[3];
int color;
};
...
std::vector<Triangle> triangles;
istringstream sin;
...
while(getline(datafile, buffer)) // Read an entire line into a buffer.
{
char comma;
triangle temp;
sin.str(buffer); // associate the buffer to the stringstream.
for(size_t i = 0; i < 3; ++i) // Process the vertexes.
sin >> temp.x[i] >> comma >> temp.y[i] >> comma;
sin >> temp.color;
triangles.push_back(temp);
}
...