我已经读取了 /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;
}
我的结构看起来像这样:
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。非常感谢您的帮助!