Hi, I have a program that reads and writes files using ctypes. When I want it to read AND write (e.g. read a file, select some stuff and write that), the library returns a 'read-open' error. I think that the pointer to the file handle for read and write point to the same address. To test that hypothesis, I wrote the simplified code below. Problem is, that I can't make it work, let alone come up with a solution. ;-( How do I tell ctypes to use a particular chunck of memory, so read and write buffers do not mutually interfere? Maybe the 'offset' parameter of ctypes.byref? import ctypes import platform import os import tempfile
# load libraries pf = platform.platform().lower() if pf.startswith("win"): libc = ctypes.cdll.msvcrt fopen = libc._fdopen elif pf.startswith("lin"): libc = ctypes.CDLL("libc.so.6") fopen = libc.fdopen elif pf.startswith("darwin"): libc = ctypes.CDLL("libc.dylib") fopen = libc.fdopen else: raise NotImplementedError # create test data path = tempfile.gettempdir() os.chdir(path) fn = "test.txt" lines = 100 * (100 * "*" + os.linesep) with open(fn, "wb") as f: f.write(lines) # read test data (code does NOT work) fh = fopen(ctypes.c_char_p(fn), "rb") fhPtr = ctypes.byref(ctypes.c_int(fh)) buff = ctypes.create_string_buffer(lines) ret = libc.fread(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fhPtr) print buff.value # write test data (code does NOT work) fn = "somefile.txt" fh_out = fopen(ctypes.c_char_p(fn), "wb") fh_outPtr = ctypes.byref(ctypes.c_int(fh_out)) buff = ctypes.create_string_buffer(lines) ret = libc.fwrite(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fh_outPtr) Thanks in advance! Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor