0

我编写了一个 Java 程序来写入ByteArray文件。结果ByteArray是这三个字节数组的结果-

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

所以我现在有一个文件,其中第一行包含生成的 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();
    }
}
4

1 回答 1

1

您似乎不想用它std::getline来读取此数据。您的文件不是逐行写入文本数据 - 它基本上具有二进制格式。

您可以使用 的read方法std::ifstream从输入流中读取任意数据块。您可能希望以二进制模式打开文件:

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)。
于 2013-09-29T02:33:38.010 回答