?
Solved

Using ctime()...

Posted on 1999-01-08
6
Medium Priority
?
280 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
  • 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 280 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 how to use strings and some functions related to them in the C programming language.
Suggested Courses

840 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