obtaining boot time in nice format

This is a discussion on obtaining boot time in nice format within the C Programming forums, part of the General Programming Boards category; Hey all, I need to convert the given boot time which is given in the /proc/stat filesystem in seconds to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    obtaining boot time in nice format

    Hey all,
    I need to convert the given boot time which is given in the /proc/stat filesystem in seconds to ctime(). Please look at this code and tell me what I am doing wrong. Thank you.
    Code:
      /* BOOT TIME */
      int boot_time;
      double location_btime;
      thisFile = fopen("/proc/stat", "r");
      while (fgets(Buffer, LB_SIZE + 1, thisFile) != NULL)
        /* while the feof has not been reached, print info line by line */
        {
          if (strstr(Buffer, "btime") != NULL){
            location_btime = ftell(thisFile);
            break;
          }
        }
      fseek(thisFile, location_btime - 12, SEEK_SET);
      fscanf(thisFile, "%d\n", &boot_time);
      printf("boot time in sec: %d\n", ctime(&boot_time));
      fclose(thisFile);

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    ftell() and fseek() deal with a long int, not a double. But instead of all the ftell()/fseek() hocuspocus I think I'd just break out of the fgets() loop and do: boot_time = atol(Buffer+6);
    Last edited by itsme86; 09-15-2004 at 05:09 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    I second that.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  3. 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
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

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