Solved

Using a string with strftime function

Posted on 2004-10-27
1,186 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
Question by:jewee
    12 Comments
     
    LVL 86

    Expert Comment

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

    Author Comment

    by:jewee
    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
    >> 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
    >>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
    oops.....sorry, I meant std::string curDateTime
    0
     
    LVL 17

    Expert Comment

    by:rstaveley
    > 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:
    >>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
    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
    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
    >>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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now