Variable in a file name

J

John Brawley

Greeting again, I've been absent a long time.
I need some help. I've searched the web to little avail.
I have a program that takes a user-entered numerical input of less than six
<long int> numbers at program start.
The program writes a file upon demand (keyboard hit).
Until now I've not needed to name the output files with the input numbers
inserted in the file names.
Now I do, and can't find out how.
I need to:

// void writftvr (double *pdb ... ) {
// ofstream tvrfile; //write a .TVR file
//tvrfile.open("tvrfile.tvr");

Right there above I need to
// tvrfile.open("tvr" +pNum+ ".tvr") ;

(For example. pNum is the <long int>.
I know that won't work.
What _will_ work?)

//if (tvrfile.is_open()) {

Thanks! If anyone knows an URL or standard reference with the info I need,
that'll do.
I can't imagine this is as difficult as it seems to be. In Python it was
easy.
 
S

saurabh

Greeting again, I've been absent a long time.
I need some help.  I've searched the web to little avail.
I have a program that takes a user-entered numerical input of less than six
<long int> numbers at program start.
The program writes a file upon demand (keyboard hit).
Until now I've not needed to name the output files with the input numbers
inserted in the file names.
Now I do, and can't find out how.
I need to:

// void writftvr (double *pdb ... ) {
// ofstream tvrfile;       //write a .TVR file
//tvrfile.open("tvrfile.tvr");

Right there above I need to
// tvrfile.open("tvr" +pNum+ ".tvr") ;

(For example.  pNum is the <long int>.
I know that won't work.
What _will_ work?)

//if (tvrfile.is_open()) {

Thanks!  If anyone knows an URL or standard reference with the info I need,
that'll do.
I can't imagine this is as difficult as it seems to be.  In Python it was
easy.

jb,
C++ is not dynamically typed language like Python,so you can not
convert an
int to a string implicitly or by just saying str(pNum).
Instead you should take a variable lets call it 'name',and construct
it using the parts you want.
for example you can do something like following

std::string fixname("tvr");
std::string fixextension(".tvr");
char varPart[7];//If you know there will be a MAX 6 digits in the
input number
snprintf(varPart,6,"%ld",pNum);//convert pNum to a string and save in
varPart
std::string finalName=fixname+fixextention+varPart;
tvrfile.open(finalName);//

Hope it helps you.

Saurabh
 
I

Ian Collins

John said:
Greeting again, I've been absent a long time.
I need some help. I've searched the web to little avail.
I have a program that takes a user-entered numerical input of less than six
<long int> numbers at program start.
The program writes a file upon demand (keyboard hit).
Until now I've not needed to name the output files with the input numbers
inserted in the file names.
Now I do, and can't find out how.
I need to:

// void writftvr (double *pdb ... ) {
// ofstream tvrfile; //write a .TVR file
//tvrfile.open("tvrfile.tvr");

Right there above I need to
// tvrfile.open("tvr" +pNum+ ".tvr") ;

The canonical answer is to use a string stream to build the name:

#include <sstream>

std::eek:stringstream out;

out << "tvr" << pNum << ".tvr";

tvrfile.open( out.str().c_str() );
 
S

saurabh

The canonical answer is to use a string stream to build the name:

#include <sstream>

std::eek:stringstream out;

out << "tvr" << pNum << ".tvr";

tvrfile.open( out.str().c_str() );

Thanks Ian,

Even after spending more than a year with c++ I am still a C
programmer from heart,thanks for the stringstream trick.
 
P

Paul N

Thanks Ian,

Even after spending more than a year with c++ I am still a C
programmer from heart,thanks for the stringstream trick.

Ditto, so I'd probably have done it more like

char buff[30];
sprintf(buff, "tvr%ld.tvr", pNum);

But don't let me hold you back.
 
J

John Brawley

Paavo Helde said:
Or, if you have Boost library installed:

#include <boost/lexical_cast.hpp>
#include <string>

tvrfile.open( ("tvr" + boost::lexical_cast<std::string>(pNum) +
".tvr").c_str() );

(If you don't have Boost, then it is not worth for this little thing, but
in general it contains tons of decent stuff one might find good use of).

hth
Paavo

Thank you, Paavo, but a) I'm not a programmer (I'm a handyman who had to
write a program), hence minimal language is fine and I don't need the boost
library, albeit I was aware of it, and b) I used Ian's method, with
excellent results.
Your answer's appreciated as well, tho'.
JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top