Solved

Using ctime()...

Posted on 1999-01-08
6
273 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Suggested Solutions

Title # Comments Views Activity
Certified Pentester Exam - need assistance on compiling an exploit 6 408
voltage to force translation ? 8 99
How to creat good software interface ? 1 68
Acrinis True image 2 92
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…
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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

829 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