I'm trying to do integrate with python and c++ now and what I'm gonna do is using c++ function in python.
The feature of function is read all text in a text file.
I know I have to make c++ code to c style, so I made c++ code like below:
void ReadTextFile(string fileName)
{
string str;
string line;
ifstream file(fileName);
while(getline(file, line))
{
vector<string> result = split(line, ':');
if (result.size() > 1)
{
result[1].erase(std::remove(result[1].begin(), result[1].end(), ' '), result[1].end());
cout << result[1] << endl;
}
}
}
extern "C" {
void ReadTextFileCpp(string fileName) {return ReadTextFile(fileName);
}
So I can get right result what I want to when I just build this c++ file.
But the problem is when I build this cpp file to .so file and call this function in python file. this is what I call the function in python:
lib2 = cdll.LoadLibrary('./ReadTextFile.so')
lib2.ReadTextFileCpp('0166_405504.txt')
The error occurs when I call the ReadTextFile function and this is the error message.
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Why this error occurs only when I call in python?
p.s) I have changed 'std::string' to 'const char*'. This code runs but it doesn't get inside of while loop.
void ReadTextFile(const char* fileName)
{
string str;
string line;
ifstream file(fileName);
while(getline(file, line))
{
vector<string> result = split(line, ':');
if (result.size() > 1)
{
result[1].erase(std::remove(result[1].begin(), result[1].end(), ' '), result[1].end());
cout << result[1] << endl;
}
}
}
extern "C" {
void ReadTextFileCpp(const char* fileName) {return ReadTextFile(fileName);
}
extern "C"function that accepts a C++ type likestd::string(some compiler implementations may allow it, but Python doesn't know how to call such things). To be legalextern "C", it has to have a prototype (arguments and return values) that are C legal. The body of the function can do C++ things, but the interface must be C-legal. In this case that should be doable at least; just acceptconst char*instead ofstd::string.ctypesdocs? You need to declare the prototypes for the function, or it assumes everything isints (which is kind of a problem when passing a pointer). It's also unclear what'ssplitis here; it could hide all sorts of issues. I'd suggest trying to test this code purely in C++ with a debugger, to make sure it works there, before trying to interface to it with Python.argtypesandrestypefor the function at the Python layer? Again, read thectypesdocs, you can't just call a function by name and hope.