ofstream SaveFile

N

newshop

How can I make the filename variable, by appending the current
timestamp to file, instead of following static file name:
ofstream SaveFile("output.txt");

example something like:
"output_<currenttimestamp>.txt"


thanks.
 
B

Bart

How can I make the filename variable, by appending the current
timestamp to file, instead of following static file name:
ofstream SaveFile("output.txt");

example something like:
"output_<currenttimestamp>.txt"

#include <sstream>
....
std::stringstream stream;
stream << "output_" << currenttimestamp << ".txt" << std::flush;
std::eek:fstream SaveFile(stream.str().c_str());

Regards,
Bart.
 
N

newshop

How can I get the current timestamp ?
#include <sstream>
...
std::stringstream stream;
stream << "output_" << currenttimestamp << ".txt" << std::flush;
std::eek:fstream SaveFile(stream.str().c_str());

Regards,
Bart.
 
K

Kai-Uwe Bux

How can I get the current timestamp ?

You want to read up on the header ctime (or the header time.h, which puts
the stuff into global namespace). It defines all the standard types and
functions that deal with time. Here is a code snippet:

#include <ctime>
#include <iostream>

int main ( void ) {
std::time_t now;
std::time ( &now );
std::cout << asctime( std::gmtime( &now ) ) << '\n';
std::cout << asctime( std::localtime( &now ) ) << '\n';
std::cout << ctime( &now ) << '\n';
}

Ah, and please do not top-post: it is frowned upon in this group. (That's a
cultural thing, you will get better responses and friendlier replies if you
adjust. The local regulars seem to feel quite strongly about it.)


Best

Kai-Uwe Bux
 
N

newshop

Kai-Uwe Bux said:
You want to read up on the header ctime (or the header time.h, which puts
the stuff into global namespace). It defines all the standard types and
functions that deal with time. Here is a code snippet:

#include <ctime>
#include <iostream>

int main ( void ) {
std::time_t now;
std::time ( &now );
std::cout << asctime( std::gmtime( &now ) ) << '\n';
std::cout << asctime( std::localtime( &now ) ) << '\n';
std::cout << ctime( &now ) << '\n';
}


Ah, and please do not top-post: it is frowned upon in this group. (That's a
cultural thing, you will get better responses and friendlier replies if you
adjust. The local regulars seem to feel quite strongly about it.)


Best

Kai-Uwe Bux


ops..sorry..

thanks for that. How can I convert the timestamp into a string format ?
because I would like to append the timestamp into my filename ? i.e
20060929171430

stream << "output_" << currenttimestamp << ".txt" << std::flush;

Thanks.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Bart said:
#include <sstream>
...
std::stringstream stream;
stream << "output_" << currenttimestamp << ".txt" << std::flush;
std::eek:fstream SaveFile(stream.str().c_str());

Is that std::flush required for any reason or is it a belts & braces
thing? If it's needed I guess I should add them a few places where we
use wstringstream, but I've never seen any problem with it.


K
 

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

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top