Reading binary files in Cython












0















I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.



A function was created that opens the file, reads in a number of data point and returns them to an array.



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))


The code compiles.



When the code is run the following error occurs:



Python has stopped working error



I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?










share|improve this question




















  • 1





    That is a crash.

    – Robert Harvey
    Nov 22 '18 at 16:19






  • 1





    Have you determined yet if you're not already reading data as fast as the hard drive can provide it?

    – Robert Harvey
    Nov 22 '18 at 16:20











  • Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?

    – MSeifert
    Nov 22 '18 at 16:22











  • Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().

    – Christopher Bridge
    Nov 22 '18 at 16:46








  • 2





    Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.

    – ead
    Nov 22 '18 at 16:59
















0















I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.



A function was created that opens the file, reads in a number of data point and returns them to an array.



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))


The code compiles.



When the code is run the following error occurs:



Python has stopped working error



I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?










share|improve this question




















  • 1





    That is a crash.

    – Robert Harvey
    Nov 22 '18 at 16:19






  • 1





    Have you determined yet if you're not already reading data as fast as the hard drive can provide it?

    – Robert Harvey
    Nov 22 '18 at 16:20











  • Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?

    – MSeifert
    Nov 22 '18 at 16:22











  • Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().

    – Christopher Bridge
    Nov 22 '18 at 16:46








  • 2





    Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.

    – ead
    Nov 22 '18 at 16:59














0












0








0


0






I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.



A function was created that opens the file, reads in a number of data point and returns them to an array.



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))


The code compiles.



When the code is run the following error occurs:



Python has stopped working error



I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?










share|improve this question
















I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.



A function was created that opens the file, reads in a number of data point and returns them to an array.



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))


The code compiles.



When the code is run the following error occurs:



Python has stopped working error



I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?







cython binaryfiles






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 21:08







Christopher Bridge

















asked Nov 22 '18 at 16:18









Christopher BridgeChristopher Bridge

42




42








  • 1





    That is a crash.

    – Robert Harvey
    Nov 22 '18 at 16:19






  • 1





    Have you determined yet if you're not already reading data as fast as the hard drive can provide it?

    – Robert Harvey
    Nov 22 '18 at 16:20











  • Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?

    – MSeifert
    Nov 22 '18 at 16:22











  • Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().

    – Christopher Bridge
    Nov 22 '18 at 16:46








  • 2





    Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.

    – ead
    Nov 22 '18 at 16:59














  • 1





    That is a crash.

    – Robert Harvey
    Nov 22 '18 at 16:19






  • 1





    Have you determined yet if you're not already reading data as fast as the hard drive can provide it?

    – Robert Harvey
    Nov 22 '18 at 16:20











  • Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?

    – MSeifert
    Nov 22 '18 at 16:22











  • Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().

    – Christopher Bridge
    Nov 22 '18 at 16:46








  • 2





    Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.

    – ead
    Nov 22 '18 at 16:59








1




1





That is a crash.

– Robert Harvey
Nov 22 '18 at 16:19





That is a crash.

– Robert Harvey
Nov 22 '18 at 16:19




1




1





Have you determined yet if you're not already reading data as fast as the hard drive can provide it?

– Robert Harvey
Nov 22 '18 at 16:20





Have you determined yet if you're not already reading data as fast as the hard drive can provide it?

– Robert Harvey
Nov 22 '18 at 16:20













Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?

– MSeifert
Nov 22 '18 at 16:22





Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?

– MSeifert
Nov 22 '18 at 16:22













Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().

– Christopher Bridge
Nov 22 '18 at 16:46







Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().

– Christopher Bridge
Nov 22 '18 at 16:46






2




2





Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.

– ead
Nov 22 '18 at 16:59





Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.

– ead
Nov 22 '18 at 16:59












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434892%2freading-binary-files-in-cython%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434892%2freading-binary-files-in-cython%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy