I'm sorry if this has been asked, but I can't seem to find a solution that helps me. I'm trying to read in data from a text file, and ultimately store it in an object.
The text file has 4 variables all separated with commas.
I've tried to do this as follows:
string v1, v2, v3, v4;
ifstream afile;
afile.open("thefile.txt");
afile >> v1 >> v2 >> v3 >> v4;
afile.close();
cout << v1 << endl;
cout << v2 << endl;
cout << v3 << endl;
cout << v4 << endl;
The file has multiple records. I have tried to just do 1 for now to makes sure it works, but when it reads in the data, it doesn't separate at the commas.
From there, I want to store the data into an object. Will the following work: Thing* thing1 = new Thing(v1, v2, v3, v4);
Although, when I read in say 5 records, what would be the best way to structure the above line of code? As each object needs a unique name, is there a way I could iterate perhaps using a for loop and a vector? ie for (int i = 0; i < 5; i++) { // read in data // store in vector }
Any hints are very much appreciated