Filnames and timestamps

Discussion in 'C Programming' started by milkyway, Sep 13, 2005.

  1. milkyway

    milkyway Guest

    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
    milkyway, Sep 13, 2005
    #1
    1. Advertising

  2. milkyway wrote:
    > 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
    David Resnick, Sep 13, 2005
    #2
    1. Advertising

  3. >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
    Gordon Burditt, Sep 13, 2005
    #3
  4. milkyway

    milkyway Guest

    Thanks but are there any sample programs of how to use this lying
    around?
    milkyway, Sep 13, 2005
    #4
  5. Please preserve some context when posting replies.

    "milkyway" <> wrote:

    [ 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
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc
    Irrwahn Grausewitz, Sep 14, 2005
    #5
  6. milkyway

    milkyway Guest

    Thank you ;-)
    milkyway, Sep 17, 2005
    #6
  7. 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++
    Emmanuel Delahaye, Sep 18, 2005
    #7
  8. 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."
    Emmanuel Delahaye, Sep 18, 2005
    #8
  9. "Emmanuel Delahaye" <> wrote:
    >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)


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

    >>>>> ERRORS_MMDDYYYY_HHMMSS


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

    Best Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc
    Irrwahn Grausewitz, Sep 19, 2005
    #9
    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. Chumma Dede
    Replies:
    2
    Views:
    395
    Chumma Dede
    Feb 11, 2005
  2. David Bernier

    help needed with filnames as command line arguments

    David Bernier, Oct 17, 2003, in forum: C Programming
    Replies:
    28
    Views:
    661
    Floyd Davidson
    Oct 27, 2003
  3. middletree

    date value and timestamps

    middletree, Nov 25, 2003, in forum: ASP General
    Replies:
    4
    Views:
    119
    dlbjr
    Nov 25, 2003
  4. Phil Tomson
    Replies:
    4
    Views:
    430
    Phil Tomson
    Jun 22, 2004
  5. Dave Saville

    Oddity with File::Find and timestamps

    Dave Saville, Dec 24, 2010, in forum: Perl Misc
    Replies:
    12
    Views:
    158
    Ilya Zakharevich
    Dec 28, 2010
Loading...

Share This Page