asctime()

This is a discussion on asctime() within the C Programming forums, part of the General Programming Boards category; I'm trying to use the function asctime() and save the function output to a string, but I can't figure out ...

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    asctime()

    I'm trying to use the function asctime() and save the function output to a string, but I can't figure out how to do it. What I've posted is the only way I can get it to compile without errors, but it's obviously wrong. Am I missing something obvious?
    Code:
    main()
    {
       time_t lt;
       struct tm t, *tmptr;
       
       lt=time(NULL);
       tmptr=localtime( & lt );
       char strings[26];
       const char *chptr=strings;
       chptr=asctime(tmptr);
       printf("%s\n%s",asctime(tmptr),strings);
       return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    You mean

    strcpy(strings,asctime(tmptr));

    You're writing C++ with those embedded declarations you know.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Thanks Salem, that was a little obvious I guess I don't work with the standard functions enough. And no, I wasn't sure if it was C++ or not, but I have seen it in my C++ book

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    #include <time.h>
    
    int main(int argc, char *argv[])                                     
    {                                          
       time_t lt;                              
       struct tm t, *tmptr;                    
       char tmp[26]={'\0'};                                         
       lt=time(NULL);                          
       tmptr=localtime( & lt );                
       strcpy(tmp,asctime(tmptr));                   
       printf("%s\n",asctime(tmptr));
       printf("%s\n",tmp);
       return 0;                               
    }
    asctime() returns a static char *, which will change with subsequent calls to time functions; this makes a local copy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. asctime woes
    By magis in forum C++ Programming
    Replies: 7
    Last Post: 06-23-2005, 04:01 PM
  3. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21