我有一个需要调用的函数,它接受一个 ostream。
我要的是一个char数组中的数据,一个char*。
所以我要做的是将 ostream 数据复制到 imageBytes 中。问题是,这对我来说似乎效率低下,我只想将数据放入 char[ONE_MEGABYTES]。将它输出到 stringstream 似乎是一种非常间接的方式。我怎样才能更有效地做到这一点?
#define ONE_MEGABYTE 1048576
volatile char* imageBytes = new char[ONE_MEGABYTE];
stringstream pngImageStringStream(ios_base::in | ios_base::out | ios_base::binary);
image.write_stream(pngImageStringStream);
imageLength = pngImageStringStream.tellp();
memcpy( (void*)imageBytes, (void*)pngImageStringStream.str().c_str(), imageLength);
最佳答案
最有效的方法是使用read
:
pngImageStringStream.read(imageBytes, imageLength);
关于c++ - 使数据进入 ostream 以进入字节数组的最有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21011241/