在boost.python中将Python二进制文件转换为C ++二进制文件时出错

问题描述 投票:0回答:2

我必须通过boost :: python将一些二进制文件从Python转换为C ++。二进制文件可能来自图像或文本文件,但是将图像文件的二进制文件转换为c ++时会发生一些错误。以下是一个示例。

C ++

#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <fstream>
#include <iostream>

using namespace boost::python;
void greet(char *name,char *ss)
{
    std::ofstream fout(name,std::ios::binary);
    std::cout<< "This length is:" << strlen(ss) <<std::endl;
    fout.write(ss.strlen);
    fout.close();
    return;
}

BOOST_PYTHON_MODULE(ctopy)
{
    def("greet",greet);
}

蟒蛇:

import ctopy
#It is right.
f=open("t1.txt","rb")
ctopy.greet("t2.txt",f.read())
f.close()

#Do a error.There isn't data in the file "p2.jpg".
f2=open("p1.jpg","rb")
ctopy.greet("p2.jpg",f2.read()) #error.the file "p2.jpg" will be a empty file.
f2.close()

如何将图像的二进制文件转换为C ++?

c++ python boost-python
2个回答
1
投票

二进制文件的编码通常取决于编程语言以外的因素,例如文件的类型,操作系统等。例如,在POSIX上 ,文本文件包含组织为零个或更多行的字符,并且在C ++和Python中都以相同的方式表示。 两种语言都只需要使用给定格式的正确编码。 在这种情况下,将Python二进制流转换为C ++二进制流没有特殊的过程,因为这是两种语言的原始字节流。

原始代码中的方法存在两个问题:

  • 应该使用strlen()确定以空值结尾的字符串的长度。 如果二进制文件包含值为\\0字节,则strlen()将不会返回数据的整个大小。
  • 数据的大小丢失了,因为使用了char*而不是std::string 考虑使用std::string因为它可以通过size()提供大小,并允许字符串本身内包含空字符。 另一种解决方案是显式地在数据旁边提供数据大小。

这是完整的Boost.Python示例:

#include <boost/python.hpp>
#include <fstream>
#include <iostream>

void write_file(const std::string& name, const std::string& data)
{
  std::cout << "This length is: " << data.size() << std::endl;
  std::ofstream fout(name.c_str(), std::ios::binary);
  fout.write(data.data(), data.size());
  fout.close();
}

BOOST_PYTHON_MODULE(example)
{
  using namespace boost::python;
  def("write", &write_file);
}

以及示例Python代码( test.py ):

import example

with open("t1.txt", "rb") as f:
    example.write("t2.txt", f.read())

with open("p1.png", "rb") as f:
    example.write("p2.png", f.read())

在用法中,我下载图像并创建一个简单的文本文件,然后使用上面的代码创建它们的副本:

[twsansbury]$ wget http://www.boost.org/style-v2/css_0/get-boost.png -O p1.png >> /dev/null 2>&1
[twsansbury]$ echo "this is a test" > t1.txt
[twsansbury]$ ./test.py 
This length is: 15
This length is: 27054
[twsansbury]$ md5sum t1.txt t2.txt
e19c1283c925b3206685ff522acfe3e6  t1.txt
e19c1283c925b3206685ff522acfe3e6  t2.txt
[twsansbury]$ md5sum p1.png p2.png
fe6240bff9b29a90308ef0e2ba959173  p1.png
fe6240bff9b29a90308ef0e2ba959173  p2.png

md5校验和匹配,表示文件内容相同。


0
投票

在从中创建了最小示例后,请提供真实代码。 此外,您使用的是哪个Python版本? 无论如何,这是您提供的代码中的一些错误:

  • 您应该使用const ,因为任何C ++常见问题解答都会告诉您。
  • 您正在对甚至不能保证以0结尾的东西使用strlen(),但它中间可能包含零。
  • 您应该使用std :: string,如果您有内部null字符,则不要使用bard。
  • 关闭文件是没有用的,这是在dtor中自动完成的。 刷新和检查流状态更加有趣。 失败时,引发异常。
  • 删除尾随收益,这没有什么坏处,但却是不必要的噪音。
  • 阅读PEP 8。
  • 使用with语句读取文件。
© www.soinside.com 2019 - 2024. All rights reserved.