By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

ofstream SaveFile

P: n/a
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.

Sep 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ne*****@gmail.com wrote:
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::ofstream SaveFile(stream.str().c_str());

Regards,
Bart.

Sep 29 '06 #2

P: n/a
How can I get the current timestamp ?

Bart wrote:
ne*****@gmail.com wrote:
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::ofstream SaveFile(stream.str().c_str());

Regards,
Bart.
Sep 29 '06 #3

P: n/a
ne*****@gmail.com wrote:
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';
}
>
Bart wrote:
>ne*****@gmail.com wrote:
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::ofstream SaveFile(stream.str().c_str());
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

Sep 29 '06 #4

P: n/a

Kai-Uwe Bux wrote:
ne*****@gmail.com wrote:
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';
}

Bart wrote:
ne*****@gmail.com wrote:
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::ofstream SaveFile(stream.str().c_str());

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.

Sep 29 '06 #5

P: n/a
On 29 Sep 2006 01:24:17 -0700 in comp.lang.c++, ne*****@gmail.com
wrote,
>How can I get the current timestamp ?
The usual answer to that is the same in C++ as it is in C, and is
covered in Steve Summit's C FAQ. It is always good to check the FAQ
before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html
Sep 29 '06 #6

P: n/a

Bart wrote:
#include <sstream>
...
std::stringstream stream;
stream << "output_" << currenttimestamp << ".txt" << std::flush;
std::ofstream 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

Sep 29 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.