Using a string with strftime function

I originally tried to concatenate a date and time stamp to a file name by using this approach.


char curDateTime[256];
time_t currentTime = time(0);
struct tm *temp_time;
temp_time = localtime(&currentTime);
strftime(curDateTime, 30, "%d%b%Y_%H", temp_time);

std::stringstream filename;

filename << pathName << "/" << fileType << curDateTime <<temp_time->tm_min <<temp_time->tm_sec;
return filename.str();

I then found out that I need to use std::string for the curDateTime.  But, when I use strftime and convert the string to a c string...I get an error saying invalid conversion..const char* to char*.
Is there something else I can do, rather than copying the string into a char*?
jeweeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>Is there something else I can do, rather than copying the string into a char*?

Youc could just use a const_cast<char*>() - but, that depends on what you are doing. I assume that the error does not occur in the above code, but elsewhere. Could you post that part?

0
jeweeAuthor Commented:
Here are the error messages:
src/FormView.cpp: In member function `std::string FormView::createFilename(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
src/FormView.cpp:1095: no matching function for call to `strftime(std::string&, int, const char[10], tm*&)'
/usr/include/time.h:75: candidates are: size_t strftime(char*, unsigned int, const char*, const tm*)
/usr/include/time.h:75:                 size_t strftime(char*, unsigned int, const char*, const tm*)

Here is the code:

std::string FormView::createFilename(std::string fileType)
{
char curDateTime[256];
time_t currentTime = time(0);
struct tm *temp_time;
temp_time = localtime(&currentTime);
strftime(curDateTime, 30, "%d%b%Y_%H", temp_time);

std::stringstream filename;

filename << pathName << "/" << fileType << curDateTime <<temp_time->tm_min <<temp_time->tm_sec;
return filename.str();
}
0
jkrCommented:
That's odd, since it should be clear to the compiler that you mean 'ize_t strftime(char*, unsigned int, const char*, const tm*)' in the above - try

::strftime((char*)curDateTime, 30, "%d%b%Y_%H", temp_time);

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jeweeAuthor Commented:
No, didn't work...here are the error messages:

src/FormView.cpp: In member function `std::string FormView::createFilename(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
src/FormView.cpp:1095: cannot convert `curDateTime' from type `std::string' to type `char*'

does it make a difference that I am building on a solaris machine?
0
itsmeandnobodyelseCommented:
>> I then found out that I need to use std::string for the curDateTime.

IMHO your first aproach works and the second could not work as std::string doesn't provide a writeable char* buffer. So, the only alternative to char buffers, i see, is not using strftime but formatting time units of temp_time yourself as you already did it with minutes and seconds.

   string months = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
   filename << pathName << "/" << fileType
               << temp_time->tm_day << months.substr(temp_time->tm_mon*4,3)
               << temp_time->tm_year+1900 <<temp_time->tm_hour
                <<temp_time->tm_min <<temp_time->tm_sec;


Regards, Alex
0
jkrCommented:
>>cannot convert `curDateTime' from type `std::string' to type `char*'

Is the above your *actual* code? At least there, that variable is defined as

char curDateTime[256];
0
jeweeAuthor Commented:
oops.....sorry, I meant std::string curDateTime
0
rstaveleyCommented:
> char curDateTime[256];

Are you sure that's what you've got? It sounds mightily like you have...

   std::string curDateTime;
0
jkrCommented:
>>I meant std::string curDateTime

There we have the problem - you cannot use a std::string as the buffer argument for 'strftime()'. The only chance I see would be to

std::string curDateTime;
char tmp[31];
time_t currentTime = time(0);
struct tm *temp_time;
temp_time = localtime(&currentTime);
strftime(tmp, 30, "%d%b%Y_%H", temp_time);

curDateTime = tmp;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
georg74Commented:
your code seems o.k. to me. the problem may be outside of it.
try following to find the problem.

#include <string>
#include <sstream>
#include <time.h>

std::string FormView::createFilename(std::string fileType)
{
      char buffer[256];
      time_t currentTime = time(0);
      struct tm *temp_time;
      temp_time = localtime(&currentTime);
//      size_t nchars = strftime(buffer, sizeof(buffer),
//            "%d%b%Y_%H", temp_time);
      size_t nchars = strftime(buffer, sizeof(buffer),
            "%d%b%Y_%H%M%S", temp_time);
      if (nchars == 0) throw int(0); /* ... some exception */
      std::string curDateTime(buffer, buffer + nchars);

      std::stringstream filename;
      filename << pathName;
      filename << "/";
      filename << fileType;
      filename << curDateTime;
//       filename << temp_time->tm_min << temp_time->tm_sec;
      std::string result = filename.str();
      return result;
}

does the error still occur? where exactly?
which compiler do you use?
which implementation of standard c++ library?

btw, avoid mixing C and C++ if possible.

HTH,
georg
0
jeweeAuthor Commented:
Defeats the purpose then.  They wanted to transition everything over to std::strings.  That was one particular case where I just couldn't get it to work.  I'll just stick with the original way.

Thanks!
0
itsmeandnobodyelseCommented:
>>src/FormView.cpp: In member function `std::string FormView::createFilename
>>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
>>src/FormView.cpp:1095: cannot convert `curDateTime' from type `std::string'
>>to type `char*'

That message means that curDateTime is defined as std::string.

You code most likely is like the following:

std::string FormView::createFilename(std::string fileType)
{
       // char curDateTime[256];
       string curDateTime;                 // HERE YOU CHANGED TO STRING
       time_t currentTime = time(0);
       struct tm *temp_time;
       temp_time = localtime(&currentTime);
       strftime(curDateTime, 30, "%d%b%Y_%H", temp_time);

       std::stringstream filename;

        filename << pathName << "/" << fileType << curDateTime
        <<temp_time->tm_min <<temp_time->tm_sec;
        return filename.str();
}

That doesn't compile as std::string cannot be converted to char*.

You can do that:

       ...
       string curDateTime = "ddMMMYYYY_HH";                
       time_t currentTime = time(0);
       struct tm *temp_time;
       temp_time = localtime(&currentTime);
       strftime((char*)curDateTime.c_str(), 30, "%d%b%Y_%H", temp_time);

However, if you are not allowed to use char buffer a type cast from std::string to char buffer isn't acceptable far more.

Regards, Alex
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.