Solved

Using ctime()...

Posted on 1999-01-08
6
274 Views
Last Modified: 2010-04-15
I cannot believe this is stumping me, but after trying several times to follow the man pages and get ctime() to work I can't seem to do so... I am looking for a short snippet which illustrates using ctime() to store the ascii version of the current date/time in a string. Surely someone can help me?

Thanks,
Iccy
0
Comment
Question by:icculus
[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
  • 2
6 Comments
 
LVL 1

Expert Comment

by:dpuckett
ID: 1255658
Let me know if this helped you, and I'll post as ANSWER

#include <time.h>
#include <stdio.h>
void main( void )
{
   time_t ltime;
   time( &ltime );
   printf( "The time is %s\n", ctime( &ltime ) );
}
0
 
LVL 1

Expert Comment

by:dpuckett
ID: 1255659
Let me know if this helped you, and I'll post as ANSWER

Sorry, re-read your question, here is stored in stringTime.

The string result produced by ctime contains exactly 26 characters and has the form of the following example:
Wed Jan 02 02:03:55 1980\n\0

#include <time.h>
#include <stdio.h>
void main( void )
{
   time_t ltime;
   time( &ltime );
   char stringTime[26];

   sprintf(stringTime,"%s",ctime(&ltime));
   printf( "The time is %s\n", stringTime );
}

0
 
LVL 1

Author Comment

by:icculus
ID: 1255660
Thats exactly what I needed to see - and it works great! Please post as an answer and I'll grade it as soon as I see the notification...

Thanks!
Andy
0
Industry Leaders: 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!

 
LVL 1

Accepted Solution

by:
dpuckett earned 70 total points
ID: 1255661
Glad I could help icculus :)

The string result produced by ctime contains exactly 26 characters and has the form of the following example:
Wed Jan 02 02:03:55 1980\n\0

#include <time.h>
#include <stdio.h>
void main( void )
{
   time_t ltime;
   time( &ltime );
   char stringTime[26];

   sprintf(stringTime,"%s",ctime(&ltime));
   printf( "The time is %s\n", stringTime );
}

0
 
LVL 1

Author Comment

by:icculus
ID: 1255662
Thanks again! Before seeing your new version (with the string char) I just enhanced what you had to use a char * - malloced with ample room. This'll work better though - thanks! :)

-Iccy
0
 
LVL 1

Expert Comment

by:dpuckett
ID: 1255663
No problem Andy, I wasn't sure what environment you were writing in and wanted to give you something ANSI compatable.
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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

726 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