- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经编写了一个 Java 程序来将 ByteArray
写入文件。结果 ByteArray
是这三个 ByteArrays 的结果-
schemaId
,我使用短数据类型表示它。Last Modified Date
,我用 long 数据类型表示它。所以我现在有一个文件,其中第一行包含生成的 ByteArray,它将具有我上面提到的所有上述字节。现在我需要从 C++ 程序读取该文件并读取将包含 ByteArray 和然后按照我上面提到的相应拆分生成的 ByteArray,以便我能够从中提取我的 schemaId
、Last Modified Date
和我的实际属性值。
我所有的编码都是用 Java 完成的,我是 C++ 的新手...我可以用 C++ 编写一个程序来读取文件,但不确定我应该如何读取 ByteArray,这样我可以像我上面提到的那样拆分它..
下面是我的 C++ 程序,它正在读取文件并将其打印到控制台上。
int main () {
string line;
//the variable of type ifstream:
ifstream myfile ("bytearrayfile");
//check to see if the file is opened:
if (myfile.is_open())
{
//while there are still lines in the
//file, keep reading:
while (! myfile.eof() )
{
//place the line from myfile into the
//line variable:
getline (myfile,line);
//display the line we gathered:
// and here split the byte array accordingly..
cout << line << endl;
}
//close the stream:
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
有人可以帮我吗?谢谢。
更新
下面是我的 Java 代码,它将生成的 ByteArray 写入一个文件,现在我需要从 C++ 读回同一个文件..
public static void main(String[] args) throws Exception {
String os = "whatever os is";
byte[] avroBinaryValue = os.getBytes();
long lastModifiedDate = 1379811105109L;
short schemaId = 32767;
ByteArrayOutputStream byteOsTest = new ByteArrayOutputStream();
DataOutputStream outTest = new DataOutputStream(byteOsTest);
outTest.writeShort(schemaId);
outTest.writeLong(lastModifiedDate);
outTest.writeInt(avroBinaryValue.length);
outTest.write(avroBinaryValue);
byte[] allWrittenBytesTest = byteOsTest.toByteArray();
DataInputStream inTest = new DataInputStream(new ByteArrayInputStream(allWrittenBytesTest));
short schemaIdTest = inTest.readShort();
long lastModifiedDateTest = inTest.readLong();
int sizeAvroTest = inTest.readInt();
byte[] avroBinaryValue1 = new byte[sizeAvroTest];
inTest.read(avroBinaryValue1, 0, sizeAvroTest);
System.out.println(schemaIdTest);
System.out.println(lastModifiedDateTest);
System.out.println(new String(avroBinaryValue1));
writeFile(allWrittenBytesTest);
}
/**
* Write the file in Java
* @param byteArray
*/
public static void writeFile(byte[] byteArray) {
try{
File file = new File("bytearrayfile");
FileOutputStream output = new FileOutputStream(file);
IOUtils.write(byteArray, output);
} catch (Exception ex) {
ex.printStackTrace();
}
}
最佳答案
您似乎不想使用 std::getline
来读取此数据。您的文件不是逐行写入文本数据 - 它基本上是二进制格式。
您可以使用std::ifstream
的read
方法从输入流中读取任意数据 block 。您可能想以二进制模式打开文件:
std::ifstream myfile("bytearrayfile", std::ios::binary);
从根本上说,从文件中读取每条记录的方法是:
uint16_t schemaId;
uint64_t lastModifiedDate;
uint32_t binaryLength;
myfile.read(reinterpret_cast<char*>(&schemaId), sizeof(schemaId));
myfile.read(reinterpret_cast<char*>(&lastModifiedDate), sizeof(lastModifiedDate));
myfile.read(reinterpret_cast<char*>(&binaryLength), sizeof(binaryLength));
这将从文件中读取数据结构的三个静态成员。因为您的数据是可变大小的,您可能需要分配一个缓冲区来将其读入,例如:
std::unique_ptr<char[]> binaryBuf(new char[binaryLength]);
myfile.read(binaryBuf.get(), binaryLength);
以上是示例,仅用于说明您将如何在 C++ 中处理此问题。您需要了解以下事项:
ifstream::read
的调用成功并返回正确数量的数据。 lastModifiedDate
字段可能需要您编写一个函数将其从 Java 使用的任何格式转换(我对 Java 一无所知)。关于java - 如何通过从 C++ 中的文件中读取来拆分 ByteArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073444/
我有以下代码,适用于文本文件,但不适用于 pdf 文件。我的文件包含英语和希腊语字符。我尝试将 pdf 文件转换为 byteStream 并将 byteStream 转换为 String 格式,以便将
我有一些大的二进制文件,我需要在其中搜索特定的字节序列,例如: find_bytes = bytearray(base64.b16decode('a226fb42')) with open(filen
如何将 bytearray 值分配给面板背景图像。如果有人有想法或经验,请帮助我克服这个问题。简要经验: 我有面板控制,想加载从 webservice 获取的图像作为背景图像。所以我使用了 setst
我的 ByteArray 值为 avroBinaryValue ,架构名称值为 String schemaName ,上次修改日期值为 long 的 lastModifiedDate 。 byte[]
我试图在开始时将一个字节数组插入到另一个字节数组中。这是我正在努力完成的一个简单示例。 import struct a = bytearray(struct.pack(">i", 1)) b = by
我正在尝试将 long 和 bytearray 连接到另一个 bytearray。 我尝试过这样的: byte[] value1= new byte[16]; byte[] value2= new b
我有一个任务,我必须在数据库中读取和保存图像。 我正在使用 C#.net Webservice 和 SQlserver2008 数据库,并将图像保存为 varbinary 格式。 我想知道 如何从本地
我在创建 ByteArray var 时遇到了一些问题,其中它的元素也是 ByteArray,我不知道是不是可能先?以及如何? 最佳答案 ByteArray 顾名思义,就是一个字节数组。如果您想保留多
我有三个 bytearray,长度为 40000。我想将字节数组 1 的字节数组索引 0,1、bytearaay2 的索引 0,1 和 bytearray3 的索引合并到 40000 长度。 像这样:
我认为这是一个新手类型的问题,但我很理解这一点。 我可以找到很多关于如何将字符串转换为各种语言的字节数组的帖子。 我不明白的是逐个字符发生了什么。我知道屏幕上显示的每个字符都由一个数字表示,例如它的
锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。 这是我的( Code Golf )挑战: 取两个字节数组并确定第二个数组是否是第一个数组的子字符
我在字节流方面做了很多工作,所以我理解这个概念——有些字节表示比其他字节更高的数值。 在阅读维基上的定义时,我变得非常困惑。 当我看到“Big-endian”或 BIG + END 这个词时,我期望最
我正在使用 Kafka 发送生产和消费消息。 生产很好,使用 制作人。 消费时,我使用下面的代码(取自示例)但我得到的每条记录只有 8 个字节(代码下方的示例输出)。 有没有一种方法可以让消费者简单地
我有很多时间创建缩略图,然后将它们转换为字节数组。我尝试了三种方法,所有 3 次都出现错误。 第一个是使用Bitmap.GetThumbnailImage,我过去用过,然后直接保存到Response.
如何将存储文件分配给 ByteArray? var file = await openPicker.PickSingleFileAsync(); 最佳答案 这里是一个使用文件选择器的示例,获取存储文件
我需要在Kotlin中创建一个包含混合类型的字节数组。 具体来说,我需要编写如下内容: 0, 1, 0x02, 1, "me@emailaddr.com" 使用Kotlin实现BLE的特性。 显然,我
当我运行这个: class SomeTest { fun howToMockByteArray() { val bytes = Mockito.mock(ByteArray::
D中ubyte[]的最大大小有限制吗? 在this线程 我遇到内存不足错误,无法理解是驱动程序问题还是 ubyte[] 大小限制? 最佳答案 D中任意数组的最大长度为size_t.max。但是,由于
如果只有一个写入器将字节附加到一个bytearray,并且多个读取器从中读取数据,那么是否有可能一个或多个读取器读取既不在扩展之前也不在扩展之后的数据? 例如,如果bytearray中的旧数据是012
请耐心听我说;我是系统管理员而不是开发人员。下面的代码对我来说效果很好。但是当我将其分成两个文件以便类位于一个文件中而逻辑位于另一个文件中时,我收到一条错误,指出 data[0] 是 str 并且不支
我是一名优秀的程序员,十分优秀!