Question about ctime for date as a string?

I have never used ctime but I would like to able to first of all take the date as a string and use it as a pathname. For example in my program I open all files in the following directory on todays date:

"c:\\test folder\\2013\\08-11"

I want ctime to provide the date for me and convert it to a string called "date" so in the case above the string first returns the year, the month and then the day in the following format.

2013\\08-11

With this I can just change my code to:

"c:\\test folder\\"+date;

Any ideas :) ?


The ctime function can't do it

But the related function strftime can
http://www.cplusplus.com/reference/ctime/strftime

This function allows you to specify the format you want using various specifiers, e.g. %Y for year, %m for month, etc.

Andy
Last edited on
Thanks for the link Andy! I can modify the example code to display the information I need but I am unsure of how I can actually create a variable string that contains the %y %d %m that I need rather than just displaying them via

[strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);

I havent been programming too long if you would be able to break down some of whats going on, I would really appreicate it!
Last edited on
After you call strftime, buffer is contains the string with the format specifiers expanded to their actual values, so you can then construct a string from the contents of buffer.
Thanks worked great :) , quick question though with

strftime (buffer,80,"\\%Y\\%m-%d",timeinfo);

how can I specify \\%Y\\%m-%d so its not actually displayed? I want the %Y %m-%d values with the \\ to be added to the buffer but not displayed.

1
2
strftime (buffer,80,"\\%Y\\%m-%d",timeinfo);
cout << buffer;


will display two lines of the same code when I only need buffer.
Last edited on
Topic archived. No new replies allowed.