Dynamic Ofstream Filenames

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
Last edited on
You can do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13

ofstream afile;
	string s= "Data";
	for(int i=0; i<10; i++){
		char str[10];
		sprintf(str,"%d.txt",i);// Convert i to a char
		string fileName=s;
		fileName.append(str);
		afile.open(fileName.c_str());
		afile<<fileName.c_str();
		afile.close();
	
	}


Agreed with Duoas, you can use ostringstream to make a file name, that is the pure c++ way
Last edited on
Ack! Don't do that! Just use a stringstream. (That is what it is for.)
1
2
3
4
5
6
7
8
9
10
11
#include <sstream>
#include <string>

...

string make_filename( const string& basename, int index, const string& ext )
  {
  ostringstream result;
  result << basename << index << ext;
  return result.str();
  }

Now you can use it directly:
1
2
3
4
  ofstream datafile;
  ...
  datafile.open( make_filename( "body", 3, ".txt" ).c_str() );
  ...


As a side note, you don't need to use a QString to write a number to cout. The standard streams know how to handle them.

I wish there was a better way than using _kbhit(). I don't think Qt supplies one... but it really is non-portable.

Anyway, I hope this helps.
Last edited on
Topic archived. No new replies allowed.