Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4074826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:13:13+00:00 2026-05-20T17:13:13+00:00

function: //http://www.cplusplus.com/reference/clibrary/ctime/strftime/ char* get_current_time() { time_t rawtime; struct tm * timeinfo; char buffer [80];

  • 0

function:

//http://www.cplusplus.com/reference/clibrary/ctime/strftime/
char* get_current_time()
{
    time_t rawtime;
    struct tm * timeinfo;
    char buffer [80];  //main.cpp:73: warning: address of local variable ‘buffer’ returned

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );

    strftime (buffer,80,"%Y %m-%d %H-%M-%S",timeinfo);
    puts (buffer);
    return buffer;
}

which is invoked by:

char* filename = get_current_time();
filename = strcat(filename, ".txt");

and the puts outputs to the console:

2011 03-18 13-51-59

so… for the most part the function works…

but when I puts (filename); after the strcat, I get this:

p??_
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:13:13+00:00Added an answer on May 20, 2026 at 5:13 pm

    Returning a LOCAL address is a bad bad bad idea.

    In C++, std::string solves most of such issues. Prefer using it.

    Also, forget about strcat, and use + or/and += with std::string.

    Re-writing your code:

    std::string get_current_time()
    {
        time_t rawtime;
        struct tm * timeinfo;
        char buffer [80]; 
    
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
    
        strftime (buffer,80,"%Y %m-%d %H-%M-%S",timeinfo);
        puts (buffer);
        return buffer; //it converts into std::string automatically!
    }
    
    std::string filename = get_current_time();
    filename += ".txt"; //concatenate. Compare it with strcat!
    

    It looks better now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to this documentation http://www.cplusplus.com/reference/clibrary/ctime/time/ for time(NULL) If the function could not retrieve the
http://www.cplusplus.com/reference/algorithm/for_each/ Unary function taking an element in the range as argument. This can either
So according to the link here: http://www.cplusplus.com/reference/algorithm/max_element/ , the max_element function is O(n), apparently
according to this site http://www.cplusplus.com/reference/std/functional/unary_function/ this code should work #include <iostream> #include <functional> using
i am reading this page http://www.cplusplus.com/doc/tutorial/exceptions.html it says if i write function() throw(); no
Based on http://www.cplusplus.com/reference/stl/vector/vector/ explicit vector ( const Allocator& = Allocator() ); This vector constructor
Is it better to use $.get(http://www.example.com/mydirectory, function(data) { $(.someclass).html(data); }); or $('.tripPlannerBottom').load(http://www.example.com/mydirectory); any speed
I'm using a COM object that has a function called GetImage. http://www.pdf-tools.com/asp/products.asp?name=P2IA When I
function checkVideoUrl(url){ var regex= [],urlmatch= false; regex[0] = /http:\/\/www.56.com\/\S+\/([^\/]+).html/i; for(i=0;i<regex.length;i++){ urlmatch =regex[i].test(url); if(urlmatch ==
This is my function http://www.ideone.com/slpPe If i don't use function preg_match_all({http://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}[^\s\\\\'\<&]*}ix, $html, $marches); $download

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.