1

I am working on project in C++ that adopts many ideas from a golang project.
I don't properly understand how this binary.write works from the documentation and how I can replicate it in C++. I am stuck at this line in my project.

binary.Write(e.offsets, nativeEndian, e.offset)

The type of e.offsets is *bytes.Buffer and e.offset is uint64

mrry
  • 125,488
  • 26
  • 399
  • 400
ArafatK
  • 740
  • 6
  • 25
  • Is `e.offsets` a file or buffer? – kennytm Mar 06 '17 at 16:02
  • @kennytm, I'm not go expert but it looks like from the docs it is: `binary.Write(destination, endian, data_to_write)` – Evan Teran Mar 06 '17 at 16:07
  • @ArafatK, is the endian aspect something you care about? – Evan Teran Mar 06 '17 at 16:07
  • @kennytm I have edited the question. – ArafatK Mar 06 '17 at 16:08
  • @EvanTeran I have read the documentation, I am looking for C++ equivalent. – ArafatK Mar 06 '17 at 16:08
  • that doesn't answer my question. Do **you** care about ensuring a specific endian? – Evan Teran Mar 06 '17 at 16:09
  • @EvanTeran OP's code clearly said "nativeEndian". – kennytm Mar 06 '17 at 16:09
  • I doubt there is a direct equivalent, since IIRC `binary.Write` uses reflection which isn't available in C++. – Ainar-G Mar 06 '17 at 16:10
  • @kennytm, right, but that may be incidental, or may be a requirement for correctness. I am trying to find out which it is. – Evan Teran Mar 06 '17 at 16:10
  • @ArafatK OK. What is the type of the corresponding `e.offset` in C++? `std::stringstream` or `std::vector` or something else? – kennytm Mar 06 '17 at 16:11
  • @kennytm Basically STRINGs are encoded as an array of 8-byte offsets so offset is number I am not sure of the exact type.(Sorry!!) – ArafatK Mar 06 '17 at 16:15
  • @ArafatK I don't get it. Perhaps you should post what you have currently translated on the C++ side. – kennytm Mar 06 '17 at 16:16
  • @kennytm The code is golang is open source. This is the link https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/tensor.go#L381 – ArafatK Mar 06 '17 at 16:17
  • @kennytm This is the link to my code. https://github.com/Arafatk/tensorflow.rb/blob/master/ext/sciruby/tensorflow_c/files/tf_tensor_helper.cc#L261 – ArafatK Mar 06 '17 at 16:19
  • @ArafatK Yes but we want to know what you have done in the C++ part. BTW why are you translating TensorFlow to C++? [It already has a C++ interface](https://www.tensorflow.org/api_docs/cc/). – kennytm Mar 06 '17 at 16:19
  • @kennytm Its for tensorflow ruby. I didn't want to create a whole story so I just asked about this subpart. – ArafatK Mar 06 '17 at 16:20
  • @kennytm Also that function crashes and because I have probably done something wrong and I am sure it has something to do with the question I have asked. – ArafatK Mar 06 '17 at 16:21

1 Answers1

1

In C++ standard libs, it is generally up to you to deal with endian concerns. So let's skip that for the time being. If you just want to write binary data to a stream such as a file, you can do something like this:

uint64_t value = 0xfeedfacedeadbeef;
std::ofstream file("output.bin", ios::binary);
file.write(reinterpret_cast<char*>(&value), sizeof(value));

The cast is necessary because the file stream deals with char*, but you can write whatever byte streams to it you like.

You can write entire structures this way as well so long as they are "Plain Old Data" (POD). For example:

struct T {
    uint32_t a;
    uint16_t b;
};

T value2 = { 123, 45 };
std::ofstream file("output.bin", ios::binary);
file.write(reinterpret_cast<char*>(&value2), sizeof(value2));

Reading these things back is similar using file.read, but as mentioned, if you REALLY do care about endian, then you need to take care of that yourself.

If you are dealing with non-POD types (such as std::string), then you will need to deal with a more involved data serialization system. There are numerous options to deal with this if needed.

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • Please check OP's comments on the question. Welp, I don't know, the question's scope seems very non-C++. – kennytm Mar 06 '17 at 16:25
  • 1
    @kennytm, I agree, the quesiton is very vague. It **seems** (not being a Go expert) that `binary.Write` does something like writing the fields of a "structure" to a stream as a basic binary serialization format. I do think that my answer (given the lack of better context) is as close as we can get for now. – Evan Teran Mar 06 '17 at 16:31