1

I've realized that open() and ioctl() does not work inside a cpp object. I am able to do the operation if it is called inside my main() function, but NOT when inside any of my classes. I have a object that is running in my main loop that has another object that makes the file system calls.

So basically when in the main loop it can open (I get a 3 for the pointer and the ioctl is successful). But when I do it in object it returns 0 for open (which isn't supposedly an error) and the ioctl fails.

I know I can't use the ios:: iostream options because they don't work with ioctl. How can I make regular ioctl work inside a cpp object?

int add=0x4b;
int i2c_bus;

if(( i2c_bus = open( "/dev/i2c-0", O_RDWR )) < 0 )
{
    printf("Unable to open file /dev/i2c-0.\n");
}

if( ioctl( i2c_bus, I2C_SLAVE, add ) < 0 )
{
    printf("Open chip %d FAILED file %d\n",add, i2c_bus);
    return -1;
}
else 
{
    printf("Open chip %d Succeeded file %d\n\n",add, i2c_bus);
    return 1;
}
1
  • open and ioctl play nicely with C++, the problem is in your code. Commented Mar 4, 2011 at 14:46

1 Answer 1

2

You've assigned the result of open to i2c_bus, but you're using fd in the ioctl. Did you change the variable names when you moved from main?

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, this example was patched together, but yes I always use the same file pointer. I am just trying 100 different ways since nothing works.
There's nothing about C++ that by itself would cause this problem. Is you application also multi-threaded? Another possibility is that something in your headers is redefining open, so that the open you are calling is not the system call you expect it to be. There isn't an open method in your class, is there? After that there's the possibility of really obscure linking problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.