1

我已经读取了 /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。非常感谢您的帮助!

4

1 回答 1

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;

当您看到 [错误:“左值需要作为赋值的左操作数] 时,您首先应该检查的是每个赋值的左侧。左值应始终解析为内存地址。变量名称是左值,可以比作对象。s->t_days 是一个左值,与 &s->t_days 不同,它可以被赋值。左值之所以这样称呼,是因为它们可以出现在赋值的左侧。右值是可以出现在右侧的值. 在这种情况下,赋值运算符的左侧有一个右值。

于 2013-10-06T17:33:03.720 回答