Python Forum
invoke c++ shared library but the FILE fread() function always return 0 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: invoke c++ shared library but the FILE fread() function always return 0 (/thread-5698.html)



invoke c++ shared library but the FILE fread() function always return 0 - sansirokarl - Oct-17-2017

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 ?


RE: invoke c++ shared library but the FILE fread() function always return 0 - Larz60+ - Oct-17-2017

Code goes between python tags not quote tags see BBCODE