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
  How to communicate between scripts in python via shared file? daiboonchu 4 2,142 Dec-31-2024, 01:56 PM
Last Post: Pedroski55
  nested function return MHGhonaim 2 1,716 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 6,020 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 4,827 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 2,197 Feb-04-2023, 12:30 PM
Last Post: caslor
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 6,525 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 10,529 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  return vs. print in nested function example Mark17 4 2,869 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 5,498 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  How to use a function from third party library? rrowhe4d 2 2,684 Aug-31-2021, 04:30 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