How do you create unique & variable file names?

I need to create random unique file names, I plan to use a value returned from a time function
to capture a value to use to name new files. My main problem is in opening a new output file stream
using a variable.

time_t timer;
string filename ;

timer = time(NULL);

filename = reinterpret_cast(timer) ;
filename.append( ".dat" );
ofstream outf ( filename , ios::out);

I know there are a number of reasons this code won't work, it's just to give you
an idea of what I want to do. The line I'm actually trying to solve is:
"ofstream outf (filename, ios::out);"
In all the examples I've found the 'filename' is a constant text value surrounded by
double quote marks. I would like to be able to use a variable there so it can be set to
the time value returned when the file was created.

Is this even possible? If not could you suggest an alternative?


Comments

  • : I need to create random unique file names, I plan to use a value returned from a time function
    : to capture a value to use to name new files. My main problem is in opening a new output file stream
    : using a variable.
    :
    : time_t timer;
    : string filename ;
    :
    : timer = time(NULL);
    :
    : filename = reinterpret_cast(timer) ;
    : filename.append( ".dat" );
    : ofstream outf ( filename , ios::out);
    :
    : I know there are a number of reasons this code won't work, it's just to give you
    : an idea of what I want to do. The line I'm actually trying to solve is:
    : "ofstream outf (filename, ios::out);"
    : In all the examples I've found the 'filename' is a constant text value surrounded by
    : double quote marks. I would like to be able to use a variable there so it can be set to
    : the time value returned when the file was created.
    :
    : Is this even possible? If not could you suggest an alternative?
    :

    There are some basic C functions called 'tmpnam' or '_tempnam' defined in stdio.h that may be what you are looking for. Try and see what you can find.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories