Reputation: 1133
I've read the value of /proc/uptime (which is the time in seconds since last boot), and I'm trying to convert it into: DD:HH:MM:SS. And I keep getting this error: "lvalue required as left operand of assignment make: *
This is my code:
/**
* Method Retrieves Time Since System Was Last Booted [dd:hh:mm:ss]
*/
int kerstat_get_boot_info(sys_uptime_info *s)
{
/* Initialize Values To Zero's */
memset(s, 0, sizeof(sys_uptime_info));
/* Open File & Test For Failure */
FILE *fp = fopen("/proc/uptime", "r");
if(!fp) { return -1; }
/* Intilaize Variables */
char buf[256];
size_t bytes_read;
/* Read Entire File Into Buffer */
bytes_read = fread(buf, 1, sizeof(buf), fp);
/* Close File */
fclose(fp);
/* Test If Read Failed Or If Buffer Isn't Big Enough */
if(bytes_read == 0 || bytes_read == sizeof(buf))
return -1;
/* Null Terminate Text */
buf[bytes_read] = '\0';
sscanf(buf, "%d", &s->boot_time);
&s->t_days = s->boot_time/60/60/24;
&s->t_hours = s->boot_time/60/60%24;
&s->t_minutes = s->boot_time/60%60;
&s->t_seconds = s->boot_time%60;
}
My structure looks something like this:
typedef struct {
/* Amount of Time Since Last Boot [dd:hh:mm:ss] */
/* Time When System Was Last Booted */
int boot_time;
int t_days;
int t_hours;
int t_minutes;
int t_seconds;
} sys_uptime_info;
I don't like using int, as it doesn't make sense in this case...I've tried double and float but when I do, I have trouble reading the value from the buf to boot_time. Your help is greatly appreciated!
Upvotes: 1
Views: 4127
Reputation: 1042
This is your issue
&s->t_days = s->boot_time/60/60/24;
&s->t_hours = s->boot_time/60/60%24;
&s->t_minutes = s->boot_time/60%60;
&s->t_seconds = s->boot_time%60;
Change to
s->t_days = s->boot_time/60/60/24;
s->t_hours = s->boot_time/60/60%24;
s->t_minutes = s->boot_time/60%60;
s->t_seconds = s->boot_time%60;
When you see [error: "lvalue required as left operand of assignment], the first thing you should check is the left hand side of each assignment. An lvalue should always resolve to a memory address. Variable names are lvalues and can be likened to objects. s->t_days is an lvalue that can be assigned a value unlike &s->t_days. lvalues are called such, because they can appear on the left hand side of an assignment. rvalues are values that can appear on the right hand side. In this case you have an rvalue on the left hand side of the assignment operator.
Upvotes: 1