Python Forum

Full Version: invoke c++ shared library but the FILE fread() function always return 0
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
this is my c code :
Quote://libtest.c
#include <stdio.h>
#include <string.h>

void testfile(const char* file)
{
FILE *fd = fopen(file, "rb");
if (!fd)
{
fprintf(stderr, "open file error");
return;
}

char buffer[12];
long reads = 0;
printf("char size is %d\n", sizeof(buffer)/sizeof(buffer[0]));
if( (reads =fread(buffer, 1, 12, fd)) != 12)
{
printf("read 12 chars error %d\n", reads);
}else {
printf("read 12 chars success %d\n", reads);
}

if (fd){
fclose(fd);
}
}
then generate a shared library:
Quote:gcc libtest.c -shared -o -fPIC -o libtest.so

then i tried to invoke the function in python:
Quote:#testfile.py
from ctypes import cdll
cur = cdll.LoadLibrary('./libtest.so')
cur.testfile('./example.wav')

but i got the result:
1, if i execute the command in python2 the fread function can be successful

Quote:python testfile.py
2, but when i tried to using python3 , the fread function always return 0
Quote:python3 testfile.py

I don't know what's wrong with this.Any suggestion for me ?
Code goes between python tags not quote tags see BBCODE