Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using a string with strftime function

Posted on 2004-10-27
12
Medium Priority
?
1,430 Views
Last Modified: 2012-06-27
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*?
0
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12423055
>>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
 

Author Comment

by:jewee
ID: 12423132
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
 
LVL 86

Expert Comment

by:jkr
ID: 12423183
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jewee
ID: 12423242
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 400 total points
ID: 12423359
>> 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
 
LVL 86

Expert Comment

by:jkr
ID: 12423624
>>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
 

Author Comment

by:jewee
ID: 12423659
oops.....sorry, I meant std::string curDateTime
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12423704
> char curDateTime[256];

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

   std::string curDateTime;
0
 
LVL 86

Accepted Solution

by:
jkr earned 1600 total points
ID: 12423743
>>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
 
LVL 3

Expert Comment

by:georg74
ID: 12423755
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
 

Author Comment

by:jewee
ID: 12423769
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12423855
>>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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question