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 :
Code:
#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];
}inforec;
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("----------------\n");
printf("FIRST: %1.2f\n", a);
printf("SECOND: %1.2f\n", b);
printf("THIRD: %1.2f\n", c);
printf("-------\n");
printf("AVERAGE: %1.2f\n\n", d);
return(0);
}
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!
djp