我已经读取了/ proc / uptime的值(这是自上次启动以来的秒数),我正在尝试将其转换为:DD:HH:MM:SS。而且我一直收到这个错误:“左值作为赋值的左操作数需要左值: *
这是我的代码:
/**
* 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;
}
/**
* 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;
}
我的结构看起来像这样:
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;
我不喜欢使用int,因为它在这种情况下没有意义...我已经尝试过double和float但是当我这样做时,我无法从buf读取值到boot_time。非常感谢您的帮助!
答案 0 :(得分:1)
这是你的问题
&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;
更改为
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;
当你看到[error:“左值作为赋值的左操作数时需要左值]时,你要检查的第一件事是每个赋值的左边。一个左值应该总是解析为一个内存地址。变量名是lvalues并且可以比较对象.s-> t_days是一个左值,可以赋予一个与& s-> t_days不同的值。左边值被称为,因为它们可以出现在赋值的左侧.rvalues是值可以出现在右侧。在这种情况下,你在赋值运算符的左侧有一个右值。