4

I have a C function of this signature:

int fileopen(const char *fname, int flags)

In Linux system, I want to pass the fname from Python using ctypes so I use ctypes.c_char_p("file1.dat") as the following:

import ctypes as ctypes

dll_name = "IO/pyaio.so"
dllabspath = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + dll_name
pyaio = ctypes.CDLL(dllabspath)

fd_w = pyaio.fileopen(ctypes.c_char_p("file1.dat"), 1)

But I get this error:

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    fd_w = pyaio.fileopen(ctypes.c_char_p("file1.dat"), 1) # 0 read, 1 write
TypeError: bytes or integer address expected instead of str instance

I don't know another way to pass the string"file1.dat" than using ctypes.c_char_p. How to solve this problem?

Thank you

1 Answer 1

5

Change "file1.dat" to b"file1.dat" (which is the same you would get with 'file1.dat'.encode('ascii')).

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

1 Comment

Brilliant. Thank you very much

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.