simple question (itoa, localtime)

Discussion in 'C Programming' started by Raskolnikow, Jul 23, 2003.

  1. Raskolnikow

    Raskolnikow Guest

    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
    Raskolnikow, Jul 23, 2003
    #1
    1. Advertising

  2. Raskolnikow

    code_wrong Guest

    bear in mind I am no expert
    comments inline

    "Raskolnikow" <> wrote in message
    news:bfkjlu$fsv1n$-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
    code_wrong, Jul 23, 2003
    #2
    1. Advertising

  3. "Raskolnikow" <> wrote (22 Jul 2003) in
    news:bfkjlu$fsv1n$-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
    Martin Ambuhl, Jul 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.hku.hk

    itoa problem?

    news.hku.hk, May 1, 2004, in forum: C++
    Replies:
    7
    Views:
    8,178
    Siemel Naran
    May 2, 2004
  2. Moritz Beller

    itoa function in gcc

    Moritz Beller, Aug 18, 2004, in forum: C++
    Replies:
    4
    Views:
    15,890
    Moritz Beller
    Aug 19, 2004
  3. jack
    Replies:
    3
    Views:
    5,021
  4. les sources de itoa

    , May 5, 2005, in forum: C++
    Replies:
    1
    Views:
    569
    red floyd
    May 5, 2005
  5. Sona

    Confusing itoa and atoi

    Sona, Sep 22, 2003, in forum: C Programming
    Replies:
    2
    Views:
    476
    Darrell Grainger
    Sep 22, 2003
Loading...

Share This Page