[Techtalk] getting the time as a string in c
Kathryn Hogg
kjh at flyballdogs.com
Thu Sep 29 09:12:34 EST 2005
kristina clair said:
> Thanks for all the replies! It was all very helpful.
>
> I did consider using ctime(), but the problem is that I do not want a
> newline in the time string.
>
> I thought that maybe I could replace it with a space, using something like
> this:
>
> time_t timeseconds;
> timeseconds = time(NULL);
> char timestring[] = ctime(×econds); /* line 13 */
> timestring[strlen(timestring)-1] = " ";
> printf("%s", ctime(&t));
If you declare timestring as "char * timestring = ...."; You'll be fine.
I prefer a somewhat safer way of replacing the trailing the newline with a
space (or just truncating):
#include <string.h>
char *timestring = ctime(time(NULL));
char *s = strrchr(timestring, '\n');
if (s != NULL)
*s = '\0';
--
Kathryn
http://womensfooty.com
More information about the Techtalk
mailing list