Time as filename

This is a discussion on Time as filename within the C Programming forums, part of the General Programming Boards category; Hi guys, this is my first post here Hope you can help me out with the problem I have... I'm ...

  1. #1
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    Time as filename

    Hi guys, this is my first post here Hope you can help me out with the problem I have...

    I'm trying to create a text file with a variabe filename. My idea was to use the time from this function:

    Code:
    /* time example */
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t seconds;
    
      seconds = time (NULL);
      printf ("%ld hours since January 1, 1970", seconds/3600);
      
      return 0;
    }
    as the filename, according to this function:

    Code:
    #include "stdio.h"
    File Input/Output C Tutorial 10-5
    main( )
    {
    FILE *fp1;
    char oneword[100],filename[25];
    char *c;
    printf("enter filename -> ");
    scanf("%s",filename); /* read the desired filename */
    fp1 = fopen(filename,"r");
    do {
    c = fgets(oneword,100,fp1); /* get one line from the file */
    if (c != NULL)
    printf("%s",oneword); /* display it on the monitor */
    } while (c != NULL); /* repeat until NULL */
    fclose(fp1);
    }
    Now this is what I got so far:

    Code:
        FILE *stream;
        
        time_t seconds;
    
        seconds = time (NULL);
    
        stream = fopen(seconds, "w");
    But when I compile this, I get the error that Im doing an "Invalid conversion from time_t to const_char.


    Has anyone got an idea how to solve this? Any help is highly appreciated, thanks in advance.

    René
    Last edited by rkooij; 03-02-2006 at 05:53 AM.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Hint: the first argument of fopen() is not supposed to be a time_t. Construct a filename using a char array. snprintf() may help you.

  3. #3
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by cwr
    Hint: the first argument of fopen() is not supposed to be a time_t. Construct a filename using a char array. snprintf() may help you.
    Thanks for your quick reply. I've been searching for about an hour but can't find anything useful about snprintf... at least not useful enough to make me understand the working principle, let alone how to use it in my code...

    Have you maybe got a link to a website that explains how snprintf works?

    Thanks again, René

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    I think you need to use strftime() if you want to format your time into a nice text string you can use as a filename.

  5. #5
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by Salem
    I think you need to use strftime() if you want to format your time into a nice text string you can use as a filename.
    Thank you very much for your hint Salem, that works perfectly!

    Code:
    time_t now;
                
        /* Time Function for filename definition */    
        now = time(NULL);
        struct tm *t = localtime(&now);
        char date_time[30];
        strftime( date_time, sizeof(date_time), "%d%m%y_%H%M%S.txt", t );
        /*****************************************/
    
        /* Open text file for writing */
        stream = fopen(date_time, "w");
    \o/

    PS Congratulations with your 10000th post!

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you could have done the same thing with simple sprintf()
    Code:
    time_t now;
                
        /* Time Function for filename definition */    
        now = time(NULL);
        struct tm *t = localtime(&now);
        char date_time[30];
        sprintf(date_time,"%02d/%02d/%04d %02d:%02d:%03d",
     
        strftime( date_time, sizeof(date_time), "%d%m%y_%H%M%S.txt", t );
        /*****************************************/

  7. #7
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by Ancient Dragon
    you could have done the same thing with simple sprintf()
    Code:
    time_t now;
                
        /* Time Function for filename definition */    
        now = time(NULL);
        struct tm *t = localtime(&now);
        char date_time[30];
        sprintf(date_time,"%02d/%02d/%04d %02d:%02d:%03d",
     
        strftime( date_time, sizeof(date_time), "%d%m%y_%H%M%S.txt", t );
        /*****************************************/

    Doesn't look simpler to me, in fact it's even got an extra line. I'll stick with what I had, thanks anyway!

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by rkooij
    Doesn't look simpler to me, in fact it's even got an extra line. I'll stick with what I had, thanks anyway!
    ignore my post -- I did something (don't know what) and the browser posted before I was done thying. I thought I deleted my post, but guess I couldn't even do that right But yes, stay with what you had. I was just suggesting an alternative.

    It was supposed to look like below.
    Code:
    sprintf(date_time,"%02d/%02d/%04d %02d:%02d:%03d",
       t->tm_day,t->tm_mon+1,t->tm_year+1900,
      t->tm_hour,t->tm_minute,t->tm_second);

  9. #9
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by Ancient Dragon
    ignore my post -- I did something (don't know what) and the browser posted before I was done thying. I thought I deleted my post, but guess I couldn't even do that right But yes, stay with what you had. I was just suggesting an alternative.

    It was supposed to look like below.
    Code:
    sprintf(date_time,"%02d/%02d/%04d %02d:%02d:%03d",
       t->tm_day,t->tm_mon+1,t->tm_year+1900,
      t->tm_hour,t->tm_minute,t->tm_second);

    Hehehe, ok that explains! Well, I understand less of your solution in comparison to the one further above. So in this case I'll still stay with the original solution because that's easier when there's changes needed in the future . I do thank you for the effort!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21