我曾经在bash中编写了一个简单的守护进程 - 也设置了/ proc / *中的值。例如,
echo 50 > /sys/class/backlight/acpi_video0/brightness
我想在C ++中重写并分享 - 但如何在C ++中使用/ proc / *?作为客户
答案 0 :(得分:3)
记住:在Unix上,一切都是文件(好吧,授予,几乎一切)。
您当前的shell代码实际上意味着:将值50(echo 50
)写入文件(redirection operator >
),其名称跟随(/sys/class/backlight/acpi_video0/brightness
)。< / p>
在C ++中,只需将/sys/class/backlight/acpi_video0/brightness
作为文件打开,然后使用您喜欢的任何方法读取/写入:C ++ fstream
,C fopen/fread/fwrite
,...
fstream
的示例(即ofstream
,因为我们只是写信给它):
std::ofstream file("/sys/class/backlight/acpi_video0/brightness");
if (!file.is_open())
throw std::runtime_error("Could not open the file");
file << 50;
file.close();
答案 1 :(得分:2)
代码示例:
int val = 50;
FILE *f = fopen("/sys/class/backlight/acpi_video0/brightness", "w");
if (!f)
{
fprintf(stderr, "Huh, couldn't open /sys/class ... ");
exit(1);
}
fprintf(f, "%d", val);
fclose(f);