473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

simple question (itoa, localtime)

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
Nov 13 '05 #1
2 4998
bear in mind I am no expert
comments inline

"Raskolnikow" <ra*********@freesurf.ch> wrote in message
news:bf************@ID-126543.news.uni-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
Nov 13 '05 #2
"Raskolnikow" <ra*********@freesurf.ch> wrote (22 Jul 2003) in
news:bf************@ID-126543.news.uni-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
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward...
10
by: Sean McIlroy | last post by:
Can anybody help me make sense of the fact that the following script doesn't work? It's so simple I can't imagine what I'm missing. Any help will be much appreciated. Peace, STM ## ALARM...
7
by: news.hku.hk | last post by:
Excuse me, i write the following function to add comma for integers but the unix server said: In function `class string comma(int)': implicit declaration of function `int itoa(...)'...
2
by: Sona | last post by:
Hi, I have a char* that holds an ascii character in its first element (at least I think that's what it holds because when I print it, it prints weird characters). I need to convert this into an...
29
by: pete | last post by:
I wrote a version of itoa yesterday. Features: 1 No implementation defined arithmetic. All of the division and modulus division is done on positive values only. 2 No object is assumed...
11
by: rayw | last post by:
I'm pretty new to C, although I did do some years ago now. I've been told that itoa is no longer a standard function, and that the ato... functions - although in the std - are not recommended. ...
24
by: Mark | last post by:
hi, all i want is a simple function that takes an int, and returns a char* so i tried char * itoa(int i) { char buff; return _itoa(i,buff,10); }
7
by: silverburgh.meryl | last post by:
Hi, Can you please tell me where I can find itoa()? I try to compile the following example, but I get the following error: .../t.cpp:20:2: warning: no newline at end of file .../t.cpp: In...
1
by: OleMacGeezer | last post by:
Hello Everyone, I am a brand new Python programmer with barely a month of experience under my belt. Here are my specs: Mac OSX Panther 10.3.9 Jython 2.1 implementation with Hermes BBS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.