[英]not able to read iio file from userspace
I am trying to read following file from C code. 我试图从C代码中读取以下文件。
file: /sys/bus/iio/devices/iio\\:device0/in_voltage7_raw
file:
/sys/bus/iio/devices/iio\\:device0/in_voltage7_raw
but file pointer I am getting is -1. 但我得到的文件指针是-1。
Using cat command it is able to read the file. 使用cat命令可以读取文件。
But I am trying to read the same from my code as follows: 但我试图从我的代码中读取相同内容如下:
nos_int32 nos_adc_read_port (ADC_PORT_DB *p_port, nos_int32 *data)
{
char file_name[VALUE_MAX];
int value;
char buffer[BUFFER_LENGTH];
char intBuffer[INT_BUFFER_LENGTH];
int fd;
sprintf(file_name, "/sys/bus/iio/devices/iio\\:device0/in_voltage7_raw");
fd = open(file_name, O_RDONLY);
if (fd == -1) {
return(-1);
}
if (read(fd, buffer, BUFFER_LENGTH) == -1) {
return(-1);
}
close(fd);
memcpy(intBuffer, buffer, BUFFER_LENGTH);
intBuffer[INT_BUFFER_LENGTH-1] = '\0';
value = atoi(intBuffer);
*data = value;
return(0);
}
After the line: fd = open(file_name, O_RDONLY);
行后:
fd = open(file_name, O_RDONLY);
value of fd is -1. fd的值为-1。 How can it be solved?
怎么解决?
Most command line shells use some characters for special actions and if you're trying to use them as their actual character, you need to prefix them with a backslash to escape them. 大多数命令行shell使用某些字符进行特殊操作,如果您尝试将它们用作实际字符,则需要使用反斜杠作为前缀来对其进行转义。 In this case, your shell needs you to escape the colon when accessing that filename.
在这种情况下,您的shell需要您在访问该文件名时转义冒号。
In C you don't have this issue so you can put in your code the filename as it truly is, such as: 在C中你没有这个问题所以你可以在你的代码中输入真正的文件名,例如:
"/sys/bus/iio/devices/iio:device0/in_voltage7_raw"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.