elanktronik elanktronik - 26 days ago 7
Linux Question

Linux open issue

I am having a problem with "open". even if there is no device named ttyACM3, it does not return an error. it always prints "no error".

What could be the problem ?

fd = open("/dev/ttyACM3", O_RDWR);
if(fd)
{
printf("no error");
}
else
{
printf("error");
}

Answer Source

This line:

if(fd)

Would evaluate to true for fd == -1, which is how open returns an error. open returns -1 on error, not 0.

In the future read the documentation more carefully :)

Also if (fd) evaluates to true for negative numbers, in case you were wondering.