我正在尝试在Cython中读取二进制文件。以前,这是在Python中工作的,但我希望加快这一过程。在编写完整的模块之前,下面的代码被用作熟悉和逻辑检查。完成本节后,该代码将扩展为读取多个400 Mb文件并进行处理。
已创建一个函数来打开文件,读取多个数据点并将它们返回到数组。
from libc.stdlib cimport malloc, free
from libc.stdio cimport fopen, fclose, FILE, fscanf, fread
def readin_binary(filename, int number_of_points):
"""
Test reading in a file and returning data
"""
header_bytes = <unsigned char*>malloc(number_of_points)
filename_byte_string = filename.encode("UTF-8")
cdef FILE *in_binary_file
in_binary_file = fopen(filename_byte_string, 'rb')
if in_binary_file is NULL:
print("file not found")
else:
print("Read file {}".format(filename))
fread(&header_bytes, 1, number_of_points, in_binary_file)
fclose(in_binary_file)
return header_bytes
print(hDVS.readin_binary(filename, 10))
代码会编译。
运行代码时,会发生以下错误:
Python has stopped working error
我已经玩了几天了。我认为有一个简单的错误,但我看不到。有任何想法吗?