Filnames and timestamps

M

milkyway

Hello out there,

I would like to create a filename where the name of the file is:

ERRORS_MMDDYYYY_HHMMSS.

Is there a way that this can be done?

Any help, hints or advice would be appreciated ;-)

TIA
 
D

David Resnick

milkyway said:
Hello out there,

I would like to create a filename where the name of the file is:

ERRORS_MMDDYYYY_HHMMSS.

Is there a way that this can be done?

Any help, hints or advice would be appreciated ;-)

TIA

Look up localtime and sprintf.

-David
 
G

Gordon Burditt

I would like to create a filename where the name of the file is:
ERRORS_MMDDYYYY_HHMMSS.

Is there a way that this can be done?

sprintf() is your friend when constructing filenames (or other
strings) from pieces, although in this case strftime() may be more
useful. And, of course, there's fopen() to actually create the
file.

If the time desired is the CURRENT time, use time() to get the time
(a time_t) and localtime() to break it down into something suitable
(a struct tm) to feed to strftime().

Gordon L. Burditt
 
I

Irrwahn Grausewitz

Please preserve some context when posting replies.


[ I would like to create a filename where the name of the file is:
ERRORS_MMDDYYYY_HHMMSS. ]
Thanks but are there any sample programs of how to use this lying
around?

A quick and dirty demo program:


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

#define FILENAMELEN 23

int main( void )
{
char filename[FILENAMELEN];
time_t curtime;
struct tm *loctimep;

curtime = time( NULL );
if ( curtime == -1 )
{
fputs( "Calendar time not available\n", stderr );
exit( EXIT_FAILURE );
}

loctimep = localtime( &curtime );
if ( loctimep == NULL )
{
fputs( "Time conversion error\n", stderr );
exit( EXIT_FAILURE );
}

if ( strftime( filename, FILENAMELEN,
"ERRORS_%m%d%Y_%H%M%S", loctimep ) == 0 )
{
fputs( "Generated filename too long\n", stderr );
exit( EXIT_FAILURE );
}

printf( "Generated filename: %s\n", filename );
exit( EXIT_SUCCESS );
}


HTH
BEst regards
 
E

Emmanuel Delahaye

milkyway wrote on 13/09/05 :
I would like to create a filename where the name of the file is:

ERRORS_MMDDYYYY_HHMMSS.

Is there a way that this can be done?

Any help, hints or advice would be appreciated ;-)

A file name is just another string.

time()
localtime()
strftime()

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
E

Emmanuel Delahaye

Irrwahn Grausewitz wrote on 14/09/05 :
if ( strftime( filename, FILENAMELEN,
"ERRORS_%m%d%Y_%H%M%S", loctimep ) == 0 )

Correct, but there is a design issue : for future chronological sort
help, I recommend to use the ISO date format

if (strftime (filename, FILENAMELEN,
"ERRORS_%Y%d%m_%H%M%S", loctimep) == 0)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
I

Irrwahn Grausewitz

Emmanuel Delahaye said:
Irrwahn Grausewitz wrote on 14/09/05 :


Correct, but there is a design issue : for future chronological sort
help, I recommend to use the ISO date format

if (strftime (filename, FILENAMELEN,
"ERRORS_%Y%d%m_%H%M%S", loctimep) == 0)

Right, I'd do the same, I just wanted to stick with the OP's
filename format specification:

Additionally, my code miserably fails if year > 9999. :)

Best Regards
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top