我如何在类中使用Boost包装的ofstream.Python到Python
How can i use ofstream in class that is wrapped with Boost.Python to Python?
我在一个月前更改了代码,我在下面描述的相同错误中卡住了。我找不到一个非常简单的例子,如何使用Boost公开一个fstream对象。Python到Python来解决我解释的问题
简而言之,我只是想公开一个类,它包含一个i/O对象与函数写/打开/关闭。在Pyton中,我想执行以下步骤:
- 开放File1 开放File2 开放File3 写入文件1
- 写入文件2
- 写入文件
- 关闭文件
C/c++代码
////// I N C L U D E S //////
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python.hpp>
#include <fstream>
#include <string.h>
////////////////////////////
using namespace std;
struct BoostXML_IO
{
ofstream File;
void writeFile(const string& strToWrite)
{
this->File.write(strToWrite.data(), strToWrite.size());
}
void openFile(const string& path)
{
this->File.open(path, ios::app);
}
void closeFile()
{
this->File.close();
}
};
BOOST_PYTHON_MODULE(BoostXML_IO)
{
using namespace boost::python;
class_<BoostXML_IO, boost::noncopyable>("BoostXML_IO")
.def("writeFile", &BoostXML_IO::writeFile)
.def("openFile", &BoostXML_IO::openFile)
.def("closeFile", &BoostXML_IO::closeFile)
;
}
这段代码总是编译没有错误,但最后在Python中,当我试图调用提示行中的一个函数时,我总是得到以下错误:
错误代码
>>> import BoostXML_IO
>>> File1 = BoostXML_IO
>>> File = BoostXML_IO.BoostXML_IO.openFileFailed to format the args
Traceback (most recent call last):
File "C:apptoolsPython25Libsite-packagespythonwinpywinidleCallTips.py", line 130, in get_arg_text
argText = inspect.formatargspec(*arg_getter(fob))
File "C:apptoolsPython25Libinspect.py", line 743, in getargspec
raise TypeError('arg is not a Python function')
TypeError: arg is not a Python function
(
提前感谢!
你的编译错误与Boost.Python无关。首先包括<string>
和<ofstream>
,然后添加using namespace std;
。这应该可以修复你得到的大多数错误。然后像这样修复你的结构声明:
struct BoostXML_IO
{
void openFile(const string& path)
{
myfile.open(path);
}
void closeFile()
{
myfile.close();
}
void WriteToFile(const char* strToWrite)
{
myfile.write(strToWrite, strlen(strToWrite));
}
private:
ofstream myFile;
};
我也不认为需要在openFile
和WriteToFile
中作为参数传递strToWrite
和path
成员时设置一个读写引用。
EDIT这个呢:
struct BoostXML_IO
{
BoostXML_IO()
{}
void writeFile(const string& strToWrite)
{
File.write(strToWrite.data(), strToWrite.size());
}
void openFile(const string& path)
{
File.open(path, ios::out);
}
void closeFile()
{
File.close();
}
private:
BoostXML_IO(const BoostXML_IO&);
BoostXML_IO& operator=(const BoostXML_IO&);
ofstream File;
};
BOOST_PYTHON_MODULE(BoostXML_IO)
{
using namespace boost::python;
class_<BoostXML_IO>("BoostXML_IO", init<>())
.def("writeFile", &BoostXML_IO::writeFile)
.def("openFile", &BoostXML_IO::openFile)
.def("closeFile", &BoostXML_IO::closeFile)
;
}
我不知道为什么你想要暴露一个ofstream
到python API。在openFile
中指定文件路径时,pathToFile
变量也是如此。但是我想我已经提到过了
相关文章:
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 尝试通过Process Pipe发送/接收到python包装器,c ++代码
- 如何在 python 包装中使用 unicode 字符串用于带有 cython 的 c++ 类?
- Swig C++ python 包装器文件解释
- 当我使用py 生成时,然后在macOS上boost.python包装器时,我会发现有关std :: string的错
- 用python包装C++代码(手动)
- 为 c++ 代码编写 python 包装器
- 将图像从Python包装器传递到C 功能
- C Python包装器中的内存泄漏
- 从Boost Python包装工厂创建一个派生的类
- 使用 Python 包装器编译 VTK
- 提升 Python 包装的虚拟类 - 子类返回错误:与C++签名不匹配
- Boost.Python 包装器中的纯虚拟重载运算符
- numpyarrayobject.h 在 Windows 中编译 SWIG python 包装器时未被 MinGW
- Boost.Python 包装原始指针但公开方法
- C++库的 Python 包装器
- 增强嵌套类的 Python 包装器 - 恢复全局范围
- 如何从hadoop python包装器运行c++可执行文件
- __str__()在打印用SWIG为Python包装的c++类时未调用
- 如何为python包装一个c++库