473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ofstream SaveFile

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
6 3004
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
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
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

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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
3
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another...
2
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str();...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
1
by: Brian Underhill via DotNetMonster.com | last post by:
Using C#, there is a SaveFile method under the .NET framework class. What do I need to include to get access to this? Is there a .NET library that you have to include? -- Message posted via...
2
by: crystalattice | last post by:
Not sure how to explain, but I'll try my best. I'm making a GUI with wxGlade. The GUI has windows that open when the "Next Page" button is pushed; the button also pickles the information that is...
3
by: magix | last post by:
Hi, I got following to savefile into Output.txt, but how can I specify it to be in specific folder/directory (not at the same directory as the executables? Currently, it will generate the...
5
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.