|
P: n/a
|
Raskolnikow
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(¤tTime);
systime = localtime(¤tTime);
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 | |
Share this Question
|
P: n/a
|
code_wrong
bear in mind I am no expert
comments inline
"Raskolnikow" <raskolnikow@freesurf.ch> wrote in message
news:bfkjlu$fsv1n$1@ID-126543.news.uni-berlin.de...[color=blue]
> 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;[/color]
You have not allowed for the string terminating null character
try char day[3];
[color=blue]
> char day[2];[/color]
same here
[color=blue]
> char month[2];[/color]
[color=blue]
> char currentDate[6];
>
> main(int argc, char **argv)
> {
> time(¤tTime);
> systime = localtime(¤tTime);
>[/color]
You are aware that itoa() is not standard ANSI C and is therefore off topic
for this newsgroup
[color=blue]
> itoa(systime->tm_mday, day, 10);
> itoa(systime->tm_mon, month, 10);[/color]
You could use strftime() to format a string in a multitude of ways for
displaying
[color=blue]
> printf("day = %s month = %s\n", day, month);
> /** QUESTION: why is [day] not the current day but something arbitrary **/[/color]
not enough space allocated for null terminated string as stated above
[color=blue]
>
> strcat(currentDate, day);
> strcat(currentDate, ".");
> strcat(currentDate, month);
> strcat(currentDate, ".");
> /** QUESTION: Is there something simpler than that to concatenate strings[/color]
?[color=blue]
> **/[/color]
yes strftime() for one way
hth
cw | | |
P: n/a
|
Martin Ambuhl
"Raskolnikow" <raskolnikow@freesurf.ch> wrote (22 Jul 2003) in
news:bfkjlu$fsv1n$1@ID-126543.news.uni-berlin.de / comp.lang.c:
[color=blue]
> 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.
>[/color]
Missing headers...
[color=blue]
> struct tm *systime;
> time_t currentTime;
> char day[2];
> char month[2];
> char currentDate[6];[/color]
All the above are too small.[color=blue]
>
> main(int argc, char **argv)
> {
> time(¤tTime);
> systime = localtime(¤tTime);
>
> 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 **/[/color]
Your clock is set incorrectly.[color=blue]
>
> strcat(currentDate, day);
> strcat(currentDate, ".");
> strcat(currentDate, month);
> strcat(currentDate, ".");
> /** QUESTION: Is there something simpler than that to concatenate
> strings ? **/
> <snip>[/color]
#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(¤tTime);
systime = localtime(¤tTime);
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 | | Post your reply Help answer this question
Didn't find the answer to your C / C++ question?
You can also browse similar questions: C / C++ | | Question stats - viewed: 2965
- replies: 2
- date asked: Nov 13 '05
|