繁体   English   中英

如何在cython中的结构中使用文件指针

[英]How to use a filepointer in a struct in cython

我正在尝试在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
};

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

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

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

到目前为止,我所做的是一个带有

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

我在这里想念什么? 开头的声明是false还是我传递指针的方式?


好的,感谢您的建议,我不厌其烦地阅读有关cython如何处理typedef的信息,并在Cython文档中找到了几个模板应该如何工作。 但是我仍然无法正确地做到这一点:

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

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具有相同的名称。 这表明没有ctypedef插入“正常”“ cdef结构”:

“如果标头为标记和typedef使用相同的名称,则将无法为其包含ctypedef,但是那样就没有必要了”

但是,这将给我:“错误使用了未定义类型»struct XDRFILE«,并且另外:“取消了对不完整类型的指针的引用”

最后一行永远都行不通。 您不能间接使用FILE* ,更不用说将其long了。

下一个错误是在头文件中。 它声明一个struct XDRFILE但不typedef ,为XDRFILE ,然后使用XDRFILEstruct的函数原型。 解决此问题后,Cython 0.19.2可以毫无问题地进行编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM