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);
}