Hello,
First off, I want to say that this website is amazing and has been a invaluable source of learning C++.
Here is my problem. I want to create a N number of different datafiles, with a different name. I create a N number of structure called 'body', and within body there is position information (floats), the body name "string", etc.
I would like a datafile associated with each structure creation.
Before I used to demand that there would be 3 bodies, and the code went something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
ofstream datafile1;
ofstream datafile2;
ofstream datafile3;
datafile1.open ("datafilebody1.txt")
datafile2.open ("datafilebody2.txt")
datafile3.open ("datafilebody3.txt")
datafile1<< "Body: " << BodyName1 << "\n";
datafile2<< "Body: " << BodyName2 << "\n";
datafile3<< "Body: " << BodyName3 << "\n";
etc. etc.
|
As you can see, there was a lot of unnecessary repetition, and all the computer science majors laughed at my code. :)
I have since changed the code to ask how many bodies there are, and create the variables accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// Setup all bodies
int numBodies = 0;
cout << "Number of Requested Bodies: ";
cin >> numBodies;
string bodyName;
if(numBodies == 0)
return;
for(unsigned int i=1;i<=numBodies;i++){
do {
cout << "Enter Requested Body Name " << QString::number(i).toStdString() << ": ";
cin >> bodyName;
if(!mocap->isBodyPresent(bodyName))
cout << "Body: [" << bodyName << "] not found.\n";
}
while(!mocap->isBodyPresent(bodyName));
cout << "Loading Body: [" << bodyName << "] \n";
bodyList.append(new ViconBody(bodyName));
}
i=0;
QLinkedListIterator<ViconBody *> iternum(bodyList);
while(iternum.hasNext()&& !_kbhit()){
ViconBody *body = iternum.next();
i++;
body->bodynumber=i;
ofstream bodynameXXXXX;
body->datafile<< "Body: " << body->name << "\n";
}
|
Where it says ofstream bodynameXXXXX, is where I would like to put a variable that changes with respect to the body that I am currently on.
How would I make a datafile associated with each body and able to write to them?
Any help is appreciated. Thanks!
Best,
Z