Need help printing time to a file.

This is a discussion on Need help printing time to a file. within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have been trying to figure out how to write the current system time to a file. I ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Need help printing time to a file.

    Hello everyone, I have been trying to figure out how to write the current system time to a file. I came across this code:

    Code:
    int main()
    {
    time_t ltime;
    ltime=time(NULL);
    printf("%s",asctime(localtime(&ltime)));
    getchar(); 
    }
    The above code works just fine, but it prints to the screen. I was wondering how I would get ltime to print to a file. Any help would be appreciated. Thanks.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You can open a file for writing with
    Code:
    FILE *OPTR;
    OPTR = fopen( "filename.txt", "w" );
    and write to it using
    Code:
    fprintf( OPTR, "This is the text I want to print to the file.\n" ); /* works just like printf, but the first argument is the file pointer*/
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    The thing is though, I'm using time() for the timestamp, and in order to do that, I need to have the asctime(localtime(&ltime)), so how do I create the asctime() function. Am I able to do it like this?:

    Code:
    int main()
    {
              FILE *OPTR;
              time_t ltime;
              fopen("filename.txt","w");
              ltime=time(NULL);
              fprintf(OPTR,"%s",asctime(localtime(&ltime)));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing the time that clocks says.
    By brack in forum C Programming
    Replies: 4
    Last Post: 09-03-2010, 03:55 PM
  2. printing time in HH:MM:SS:MS format
    By msenthil in forum Linux Programming
    Replies: 3
    Last Post: 10-24-2007, 01:03 PM
  3. Date and time stamp in printing to a file
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-24-2007, 02:59 AM
  4. Printing a string with a time delay
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2004, 08:08 AM
  5. Printing 20 lines at a time
    By csmatheng in forum C Programming
    Replies: 5
    Last Post: 04-30-2002, 04:11 PM

Tags for this Thread


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