Python Forum
invoke c++ shared library but the FILE fread() function always return 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
invoke c++ shared library but the FILE fread() function always return 0
#1
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 ?
Reply
#2
Code goes between python tags not quote tags see BBCODE
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 624 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,316 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 835 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,193 Feb-04-2023, 12:30 PM
Last Post: caslor
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,178 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,064 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  return vs. print in nested function example Mark17 4 1,758 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,354 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  How to use a function from third party library? rrowhe4d 2 1,874 Aug-31-2021, 04:30 PM
Last Post: Larz60+
  Override a library function that is not in __all__ Weird 7 3,299 Aug-23-2021, 05:03 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020