Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 递归ctypes结构accesing_Python_C - Fatal编程技术网

Python 递归ctypes结构accesing

Python 递归ctypes结构accesing,python,c,Python,C,对于while循环的第一次迭代,我将得到fname、ftime、fsize的值。 但是对于第二次迭代,我得到了下面的错误。 错误。”LP_flistStruct'object没有属性“fname”在C中没有递归结构,作为成员的是指向结构的指针。只需使用 class flistStruct(ctypes.Structure): """This is repos struct which is used in c code"""

对于while循环的第一次迭代,我将得到fname、ftime、fsize的值。 但是对于第二次迭代,我得到了下面的错误。
错误。”LP_flistStruct'object没有属性“fname”

在C中没有递归结构,作为成员的是指向结构的指针。只需使用

class flistStruct(ctypes.Structure): 
    """This is repos struct which is used in c code"""
    pass
flistStruct._fields_ = [('fname', ctypes.c_char * 257),
            ('ftime', ctypes.c_char * 257),
            ('fsize', ctypes.c_double),
            ('next1',ctypes.POINTER(flistStruct))]
repolistfun.DirRepository.argtypes = [ctypes.c_char_p,ctypes.POINTER(flistStruct)]
        repolistfun.DirRepository.restype = ctypes.c_int

def func():
    mylist = flistStruct()
    ret2 = repolistfun.DirRepository(bytes(arg1, encoding='utf8'),ctypes.byref(mylist))
    while mylist != None:
        result += "fname:"+str(mylist.fname)
        result += "ftime:"+str(mylist.ftime)
        result += "fsize:"+str(mylist.fsize)
        mylist = mylist.next1
但是,请注意您的
mylist!=没有一个是假的。没有一个ctype会像那样等于
None

相反,您需要根据需要重新构造循环

mylist = mylist.next1.contents

最后,一些风格上的建议:您的变量命名已关闭-mylist不是列表,而是列表中的节点/元素。和
“fsize:“+str(mylist.fsize)
似乎写得很有创意,怎么样

while True:
    result += "fname:"+str(mylist.fname)
    result += "ftime:"+str(mylist.ftime)
    result += "fsize:"+str(mylist.fsize)
    if not mylist.next1:
        break

    mylist = mylist.next1.contents
results = []

    ...
    results.append(f"fname:{node.fname} ftime:{node.ftime} fsize:{node.size}")

result = " ".join(results)