Skip to content

问题linux & C #9

@QiuShuiDyp

Description

@QiuShuiDyp

这是一个函数,具体功能是获取硬盘的序列号,但是只能输出,并不能存储下来,具体的是使用shell的system()函数进行操作,关键语句是:
snprintf(command, sizeof(command), "sudo hdparm -i %s | grep SerialNo > %s | tee -a 2.log", disk_name.c_str(), hdparm_result); //将命令写入commond中
num=system(command); //调用system()函数,屏幕输出硬盘序列号

函数代码:
static bool get_disk_serial_by_way_3(const std::string & disk_name,std::string & serial_no)
{
int num;
printf("\n--------------6---------------\n");
serial_no.c_str();
const char * hdparm_result = ".hdparm_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "sudo hdparm -i %s | grep SerialNo > %s | tee -a 2.log", disk_name.c_str(), hdparm_result);
num=system(command);
printf("the value of system is %d.\n",num);
if (0 == system(command))//Judge function system() whether come into effect
{

    get_serial(hdparm_result, "SerialNo=", serial_no);
}
unlink(hdparm_result);

return(!serial_no.empty());

}

问了一些人,有人说将commond的内容改成
sudo hdparm -i %s | grep SerialNo > %s | tee -a 2.log 即将信息写入到2.log文本,然后通过读取2.log文本来实现读取存储到序列号来

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions