Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > simple question (itoa, localtime)

Reply
Thread Tools

simple question (itoa, localtime)

 
 
Raskolnikow
Guest
Posts: n/a
 
      07-23-2003
Hi!

I have a very simple problem with itoa() or the localtime(...).
Sorry, if it is too simple, I don't have a proper example.
Please have a look at the comments.

struct tm *systime;
time_t currentTime;
char day[2];
char month[2];
char currentDate[6];

main(int argc, char **argv)
{
time(&currentTime);
systime = localtime(&currentTime);

itoa(systime->tm_mday, day, 10);
itoa(systime->tm_mon, month, 10);
printf("day = %s month = %s\n", day, month);
/** QUESTION: why is [day] not the current day but something arbitrary **/

strcat(currentDate, day);
strcat(currentDate, ".");
strcat(currentDate, month);
strcat(currentDate, ".");
/** QUESTION: Is there something simpler than that to concatenate strings ?
**/
<snip>

Thank you !

Brad


 
Reply With Quote
 
 
 
 
code_wrong
Guest
Posts: n/a
 
      07-23-2003
bear in mind I am no expert
comments inline

"Raskolnikow" <(E-Mail Removed)> wrote in message
news:bfkjlu$fsv1n$(E-Mail Removed)-berlin.de...
> Hi!
>
> I have a very simple problem with itoa() or the localtime(...).
> Sorry, if it is too simple, I don't have a proper example.
> Please have a look at the comments.
>
> struct tm *systime;
> time_t currentTime;


You have not allowed for the string terminating null character
try char day[3];

> char day[2];


same here

> char month[2];



> char currentDate[6];
>
> main(int argc, char **argv)
> {
> time(&currentTime);
> systime = localtime(&currentTime);
>


You are aware that itoa() is not standard ANSI C and is therefore off topic
for this newsgroup

> itoa(systime->tm_mday, day, 10);
> itoa(systime->tm_mon, month, 10);


You could use strftime() to format a string in a multitude of ways for
displaying

> printf("day = %s month = %s\n", day, month);
> /** QUESTION: why is [day] not the current day but something arbitrary **/


not enough space allocated for null terminated string as stated above

>
> strcat(currentDate, day);
> strcat(currentDate, ".");
> strcat(currentDate, month);
> strcat(currentDate, ".");
> /** QUESTION: Is there something simpler than that to concatenate strings

?
> **/


yes strftime() for one way

hth
cw


 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-23-2003
"Raskolnikow" <(E-Mail Removed)> wrote (22 Jul 2003) in
news:bfkjlu$fsv1n$(E-Mail Removed)-berlin.de / comp.lang.c:

> Hi!
>
> I have a very simple problem with itoa() or the localtime(...).
> Sorry, if it is too simple, I don't have a proper example.
> Please have a look at the comments.
>


Missing headers...

> struct tm *systime;
> time_t currentTime;
> char day[2];
> char month[2];
> char currentDate[6];


All the above are too small.
>
> main(int argc, char **argv)
> {
> time(&currentTime);
> systime = localtime(&currentTime);
>
> itoa(systime->tm_mday, day, 10);
> itoa(systime->tm_mon, month, 10);
> printf("day = %s month = %s\n", day, month);
> /** QUESTION: why is [day] not the current day but something
> arbitrary **/


Your clock is set incorrectly.
>
> strcat(currentDate, day);
> strcat(currentDate, ".");
> strcat(currentDate, month);
> strcat(currentDate, ".");
> /** QUESTION: Is there something simpler than that to concatenate
> strings ? **/
> <snip>


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void)
{
struct tm *systime;
time_t currentTime;
char currentDate[7];

time(&currentTime);
systime = localtime(&currentTime);

printf("day = %d month = %d\n", systime->tm_mday,
1 + systime->tm_mon);
sprintf(currentDate, "%d.%d", systime->tm_mday,
1 + systime->tm_mon);
puts(currentDate);

return 0;
}

What was your question?


--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments