C函数工作,c++版本不

C function works, C++ version does not

本文关键字:版本 c++ 函数 工作      更新时间:2023-10-16

必须使用内核来导出GPIO引脚。这是linux,所以写入文件很简单。

c++不能工作:

std::ofstream file("/sys/class/gpio/export");
file << gpio;  // an int value
C:

int fd, len;
char buf[MAX_BUF];
fd = open("/sys/class/gpio/export", O_WRONLY);
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);

有什么建议吗?

如果变量是单字节,它将输出为单个char而不是十进制数。许多字符是不可打印的,所以您可能看不到输出—请对文件进行二进制转储以确保。

修复:

file << (int) gpio;