python - Reading and writing an array in a file using C functions in cython -


i instantiating class in cython. want declare 1 of instance array of float values once computing given function , save in binary file using c functions. further call of class if input file exist, instance declared reading array file otherwise compute again.

import numpy np cimport numpy np cimport cython  libc.stdio cimport file, fopen, fwrite, fscanf, fclose, fseek, seek_end, ftell, stdout, stderr cdef extern "math.h":     double exp(double) nogil     double log(double) nogil  cdef class hit(object):     cdef public double[::1] zs, da     cdef char* path      def __cinit__(self, zs=none, path=none):         if path none:            raise valueerror("could not find path file contains table of distances")         else:            self.path=path         if zs none:            raise valueerror("you must give array contains steps!")         self.zs=zs         cdef py_ssize_t i, n         n=len(self.zs)         cdef file *ptr_fr         cdef file *ptr_fw         cdef double[::1] ptr_d = np.empty((n,))         ptr_fr = fopen(self.path, "rb")         ptr_fw = fopen(self.path, "wb")         if (ptr_fr==null):            print "i/o error: cannot open file {}".format( self.path)              n > >= 0:                ptr_d[i]=log(self.zs[i]+1.) /(1- self.zs[i])**0.5                if (ptr_fw == null):                print "unable open file!\n"            else:                print "opened file writing.\n"                fwrite(<void*>&ptr_d[0], sizeof(double), n, ptr_fw)                fclose(ptr_fw)                self.da = ptr_d          else:             n > >= 0:                fscanf(ptr_fr,"%f", &ptr_d[i])             fclose(ptr_fr)            self.da = ptr_d 

when run code second time values returns reading file pointer correct, think way allocated pointer memoryview has problem since values in self.da instance zeros. suggestion ?!!

you should change line:

ptr_fw = fopen(self.path, "wb") 

for

ptr_fw = fopen(self.path, "ab") 

see http://www.cplusplus.com/reference/cstdio/fopen/

[w option] write: create empty file output operations. if file same name exists, contents discarded , file treated new empty file.

while

[a option] append: open file output @ end of file. output operations write data @ end of file, expanding it. repositioning operations (fseek, fsetpos, rewind) ignored. file created if not exist.

the brackets mine. deleting content in path once opened it. next time, opened empty file.

update: (i did both of these changes on first implementation)

plus, change your

fwrite(<void*>&ptr_d[0], sizeof(double), n, ptr_fw) 

for:

       in range(n):            fprintf(ptr_fw, "%lf\n", ptr_d[n-i-1]) 

you reading formated writting unformatted (remember cimport fprintf stdio, , change above important).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -