[英]Linux daemon set /proc/
我曾经在bash中编写了一个简单的守护程序-还在/ proc / *中设置了值。 例如,
echo 50 > /sys/class/backlight/acpi_video0/brightness
我想用C ++重写并共享-但是如何在C ++中使用/ proc / *? 作为客户
请记住:在Unix上,所有东西都是文件(当然, 几乎所有东西)。
您当前的shell代码实际上意味着:将值50( echo 50
) 写入文件 ( redirection operator >
), 该文件 /sys/class/backlight/acpi_video0/brightness
名称( /sys/class/backlight/acpi_video0/brightness
)。
在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();
代码示例:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.