如何在cython中的结构中使用filepointer

2024-06-28 19:39:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在cython中使用c库来实现一种特殊的文件结构:

struct XDRFILE
{
    FILE *   fp;       //< pointer to standard C library file handle
    void *   xdr;      //< pointer to corresponding XDR handle
    char     mode;     //< r=read, w=write, a=append
    int *    buf1;     //< Buffer for internal use
    int      buf1size; //< Current allocated length of buf1
    int *    buf2;     //< Buffer for internal use
    int      buf2size; //< Current allocated length of buf2
};

以及打开和关闭此类对象的方法:

^{pr2}$

现在我希望能够访问这个结构中的文件指针,并将其传递给fseek和ftell来操作文件指针的位置。

到目前为止,我所做的是一个cython文件

from cython.view cimport array as cvarray
import numpy as np
cimport numpy as np
from libc.stdio cimport FILE, fseek
cdef extern from "xdrfile.h":
    ctypedef struct XDRFILE:
        FILE*   fp       #< pointer to standard C library file handle
        void*   xdr      #< pointer to corresponding XDR handle       
        char    mode     #< r=read, w=write, a=append                 
        int*    buf1     #< Buffer for internal use                   
        int     buf1size #< Current allocated length of buf1              
        int*    buf2     #< Buffer for internal use                   
        int     buf2size #< Current allocated length of buf2

    XDRFILE* xdrfile_open(const char* path, const char* mode)
    int xdrfile_close(XDRFILE* xfp)
    int xdrfile_read_int(int* ptr, int ndata, XDRFILE* xfp)

def Read( filepath ):
    cdef XDRFILE* FH = xdrfile_open(filepath, "r")
    cdef np.ndarray[np.int32_t, ndim=1] Header = np.zeros( 23, dtype=np.int32 )

    xdrfile_read_int( <int*> Header.data, 23, FH)
    print Header ## up to here everything works just fine

    #now here starts the problem:
    fseek( FH[0].fp, 159240, 0)  
    fseek( FH.fp   , 159240, 0)
    # does not work: "dereferencing pointer of incomplete type"

    ## even more brutal things do not work:
    FH.fp[0] += 159240

我错过了什么?开头的声明是假的还是我传递指针的方式?


好吧,谢谢你的建议,我已经很累地读了一下cython handels是如何打字的 在Cython documentation中,有几个公式说明了它应该如何工作。但我还是没能把它做好:

我认为这并不重要,但这是确切的结构: 有

xdrlib.c:

struct XDRFILE
{
    FILE *   fp;       //< pointer to standard C library file handle
    void *   xdr;      //< pointer to corresponding XDR handle
    char     mode;     //< r=read, w=write, a=append
    int *    buf1;     //< Buffer for internal use
    int      buf1size; //< Current allocated length of buf1
    int *    buf2;     //< Buffer for internal use
    int      buf2size; //< Current allocated length of buf2
};

xdrlib.h:

typedef struct XDRFILE XDRFILE;
XDRFILE* xdrfile_open(const char* path, const char* mode)
int xdrfile_close(XDRFILE* xfp)

现在我想用Cython修改一下,这样我就可以:

cdef XDRFILE* FH = xdrfile_open(filepath, "r")
fseek(FH.fp, 1000, 0)

在我看来,这将是我之前发布的链接中描述的最后一个实例,因此struct和typedef具有相同的名称。这表明在“normal”“cdef struct”中没有ctypedef,解释如下:

"If the header uses the same name for the tag and typedef, you won’t be able to include a ctypedef for it – but then, it’s not necessary"

但是这会给我带来:“未定义类型的错误用法»struct XDRFILE«” 另外:“取消引用指向不完整类型的指针”


Tags: oftoforbufferstructintinternalhandle
1条回答
网友
1楼 · 发布于 2024-06-28 19:39:30

最后一行行不通。你不能间接地使用FILE*,更不用说向它添加一个long。在

下一个错误在头文件中。它声明了一个struct XDRFILE,但没有typedefXDRFILE,然后在函数原型中使用XDRFILE,而不使用struct。修复后,cython0.19.2编译它时不会出现问题。在

相关问题 更多 >