Oct-17-2017, 03:08 AM
this is my c code :
then i tried to invoke the function in python:
but i got the result:
1, if i execute the command in python2 the fread function can be successful
I don't know what's wrong with this.Any suggestion for me ?
Quote://libtest.cthen generate a shared library:
#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);
}
}
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.py2, 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 ?