Python Forum
Can't open/read txt file in C extension for Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't open/read txt file in C extension for Python
#1
Hello everyone,

I'm wrapping some C++ in Python using ctypes.
I have the following problem:
When I'm trying to open/read a txt file by calling a void method in .dll file from Python (no arguments passed from or to python), program fails. Terminal either "hangs" like it's waiting for input or it just passes through without carrying out the open file instructions.

Here is the bare bones example of problematic code
The C++ example.cpp file: (I used python code tags cause there aren't any others to use for code other than Python)

#include <sdtio.h>

extern "C" {
    __declspec(dllexport) void test(){
    File* f_name;
    f_name = fopen("file.txt", "r");     //the "file.txt is in directory"
    //... doesn't goes past this^ line
    }
}
C++ code is compiled by cygwin64 to example.dll

The python ctypes wrapper:

import ctypes

lib = ctypes.CDLL("D:\Code Blocks\C_cwiczenia\stack_over\example.dll")
void_acc = lib.test
void_acc.restype = None
void_acc()
print "program ended !" # this is just so I can see if program executed
As mentioned in C++ code snippet, program doesn't get passed the fopen() line so no "program ended !" shows in Python console.
I tried this with Python 2 and 3 (64bit) on Windows. In both cases issue is thesame.
I belive this has something to do with C++ vs Python streams but I don't know what and I don't know how to get around that.

My question is:
How to open/read text file by calling a funcion in .dll from Python wrapper ?

Any help appreciated.
Reply
#2
Did fopen return a non NULL file pointer?
Reply
#3
As far as I know it did not return Null.
When I compile the C++ code into a normal exe it open/reads/prints file no problem.
Issue occurs only when Python code is calling C++ function in dll.
Reply
#4
Instead of returning void, you could cast f_name to long and return it to check from python code that a non zero value was returned. This would be a first information about what is really happening. Also, do you fclose() the file later in the code?
Reply
#5
There is nothing to fclose() cause .dll code execution stops immediately on file fopen(). That's the issue cause even if I try to return something to python I don't think it'll pass fopen() point.
To make matters worse, if I try to use any kind of "printing" (fprint(), std::cout<< ) to console from inside dll code, program stops execution like in fopen() case or it throws
Error:
WindowsError: exception: access violation when reading 0x0000000000
(or sometimes 0xFFFFFFFFFFFFF).
Reply
#6
I'm allergic to the Windows OS, all I can do is give you a complete example that works for me in Linux.

This is file paillasse/example.cpp (Note that I had to write FILE instead of File)
#include <stdio.h>
 
extern "C" {
    void test(){
        FILE* f_name;
        f_name = fopen("paillasse/test.txt", "r");
        printf("f_name is %p\n", f_name);
        fclose(f_name);
    }
}
Now on the console
Output:
$ g++ --shared -fPIC -o example.so paillasse/example.cpp $ python3 Python 3.5.2 (default, Apr 16 2020, 17:47:17) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes >>> lib = ctypes.CDLL('./example.so') >>> void_acc = lib.test >>> void_acc.res_type = None >>> void_acc() f_name is 0x226c600 0 >>>
It is working very well.
Reply
#7
Thank you very much for your help.
I haven't tried it on Linux yet. I'll setup v-box and test it but I'm pretty sure it'll work.
That gave me some clues: it's a Windows problem after all :/
I don't dare to ask you to try to make it work on Windows... however if you have any suggestions on why it's not working under WindowsOS and/or what could I do to try to make it work? I'll be much gratefull. Also I'm curious why Win fails in that matter.
Oh yes, it's "FILE" in original code, I made a mistake here, sorry.
Reply
#8
Yup. Works there (Linux). +1
What is Linux doing differently than Windows that it allows for this to work? -.- I'm puzzled.
Reply
#9
[SOLVED]

It was compiler issue after all.

Instead of cygwin I should've been using mingw-64 bit.
If anyone needs it you can get it from here:
https://www.msys2.org/

With installation I followed instructions in this yt video:
https://www.youtube.com/watch?v=aXF4A5UeSeM

Now it works fine.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 333 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,130 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Recommended way to read/create PDF file? Winfried 3 2,895 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,455 Nov-09-2023, 10:56 AM
Last Post: mg24
  Python C Extension Module loading issue on Cygwin mesibo 0 652 Sep-22-2023, 05:41 AM
Last Post: mesibo
  How can i combine these two functions so i only open the file once? cubangt 4 867 Aug-14-2023, 05:04 PM
Last Post: snippsat
  read file txt on my pc to telegram bot api Tupa 0 1,127 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,115 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,287 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,630 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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