Thread: using return from UNIX command

  1. #1
    Registered User
    Join Date
    Jun 2003

    using return from UNIX command

    Well, I really tried to avoid this, but... I searched all over the place, looked at tutorials, etc., but I can't find an example of how to do what I'm trying to do. It could be because I'm going about it all wrong; I'll be the first to admit I'm pretty green.

    I am trying to take the return from the UNIX "uptime" command, get the three float numbers and the end, and average them. It's mostly an exercise (I need to write a bunch of stuff interacting with the OS and until I can figger this out, I can't).

    I know that if I do:

    char upt[80];
    *upt = system("uptime");
    printf("%s\n", upt);

    I can print the whole line, which resembles:

    1:14pm up 99 days, 2:49, 32 users, load average: 0.17, 0.37, 0.35

    What I'm trying to do, misguided or not, is to load that information somehow into a struct, convert the salient items from the struct to float, then do the average. What I cannot do is figure out how to get it into the struct. I've learned that a struct is not an lvalue, so I can't simply load the info directly to the struct. So, am I wasting my time, and should I be doing something else based on string operations? Silly kindergarten code follows, please don't whip me :

    #include <stdlib.h>
    #include <stdio.h>
    Return from UNIX "uptime" command appears generally as follows:
      1:14pm  up 99 days,  2:49,  32 users,  load average: 0.17, 0.37, 0.35
    float a = 0;
    float b = 0;
    float c = 0;
    float d = 0;
    typedef struct
       char filler_a[55];
       char first[4];
       char filler_b[2];
       char second[4];
       char filler_c[2];
       char third[4];
    int main()
       inforec numbers;
       *numbers = system("uptime");  /* Where I'm going wrong */
       a = atof(numbers.first);
       b = atof(numbers.second);
       c = atof(numbers.third);
       d = (( a + b + c ) / 3 );
       printf("FIRST:   %1.2f\n", a);
       printf("SECOND:  %1.2f\n", b);
       printf("THIRD:   %1.2f\n", c);
       printf("AVERAGE: %1.2f\n\n", d);
    I'm not asking for code; don't think I'm asking anyone to write something. Just looking for advice and suggestions on where I'm missing the boat here. Thanks!


  2. #2
    Registered User
    Join Date
    Jun 2003

    Thank you, that is exactly what I needed. Sorry about the mis-post, I'll go play on the other board. Great FAQ, btw, lots of helpful stuff.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM