Solved

Using ctime()...

Posted on 1999-01-08
6
271 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

867 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

16 Experts available now in Live!

Get 1:1 Help Now