java - 如何通过从 C++ 中的文件中读取来拆分 ByteArray?

标签 java c++ c arrays

我已经编写了一个 Java 程序来将 ByteArray 写入文件。结果 ByteArray 是这三个 ByteArrays 的结果-

  • 2 个字节 是我的 schemaId,我使用短数据类型表示它。
  • 接下来的 8 字节 是我的Last Modified Date,我用 long 数据类型表示它。
  • 剩余字节的大小可以是可变的,这是我的属性的实际值..

所以我现在有一个文件,其中第一行包含生成的 ByteArray,它将具有我上面提到的所有上述字节。现在我需要从 C++ 程序读取该文件并读取将包含 ByteArray 和然后按照我上面提到的相应拆分生成的 ByteArray,以便我能够从中提取我的 schemaIdLast 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::ifstreamread 方法从输入流中读取任意数据 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 的调用成功并返回正确数量的数据。
  • Endianness可能是一个问题,具体取决于数据来源和读取的平台。
  • 解释 lastModifiedDate 字段可能需要您编写一个函数将其从 Java 使用的任何格式转换(我对 Java 一无所知)。

关于java - 如何通过从 C++ 中的文件中读取来拆分 ByteArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073444/

相关文章:

c++ - 构造函数失败后初始化 boost::asio 套接字

c++ - VS 2015 : The breakpoint will not currently be hit. 没有为此文档加载符号

java - request.getParameter 正在检索 'null'

java - 使用 BatchUpdateExcpetion 处理 JDBC BatchUpdate 异常

c++ - 用 enum/int 覆盖函数

c - UDP 端口扫描器问题,C

c - 无限循环后我需要返回语句吗?

c++ - OpenMp - 每个线程的循环/数组边界

java - 如果服务正在运行,如何在晚上 8 点停止该服务?

java - 如何使用Java实现简单的密码登录