Python 使用ctypes将字符串作为字符指针数组传递
我拥有此签名的Python 使用ctypes将字符串作为字符指针数组传递,python,python-3.x,ctypes,Python,Python 3.x,Ctypes,我拥有此签名的C功能: int fileopen(const char*fname,int标志) 在Linux系统中,我想使用ctypes从Python传递fname,因此我使用ctypes.c\u char\p(“file1.dat”)如下: import ctypes as ctypes dll_name = "IO/pyaio.so" dllabspath = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + dll_n
C
功能:
int fileopen(const char*fname,int标志)
在Linux系统中,我想使用ctypes
从Python传递fname
,因此我使用ctypes.c\u char\p(“file1.dat”)
如下:
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)
但我得到了这个错误:
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
回溯(最近一次呼叫最后一次):
文件“test.py”,第21行,在
fd_w=pyaio.fileopen(ctypes.c_char_p(“file1.dat”),1)#0读,1写
TypeError:需要字节或整数地址,而不是str实例
除了使用ctypes.c\u char\u p
之外,我不知道传递字符串“file1.dat”的其他方法。如何解决这个问题
谢谢将“file1.dat”
更改为b“file1.dat”
(这与'file1.dat'.encode('ascii')
相同)。。非常感谢你