# How to write, read file using bin\_prot?

**URL:** <https://discuss.ocaml.org/t/how-to-write-read-file-using-bin-prot/1961>\
**Category:** Learning\
**Tags:** serialization\
**Created:** [May 7, 2018, 2:44am UTC](https://discuss.ocaml.org/t/how-to-write-read-file-using-bin-prot/1961 "2018-05-07T02:44:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mars0i](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mars0i/32/1058_2.png) [@mars0i](https://discuss.ocaml.org/u/mars0i)\
**Post date:** [May 7, 2018, 2:44am UTC](https://discuss.ocaml.org/t/how-to-write-read-file-using-bin-prot/1961/1 "2018-05-07T02:44:27Z")

</div>

I’m confused about how to use `bin_prot` to write and read serialized data to/from a file.

[`bin_prot`](https://github.com/janestreet/bin_prot) has many functions with “write” and “read” in their names, and some return functions with type `writer`, etc. (see [`Bin_prot.Write`](https://ocaml.janestreet.com/ocaml-core/latest/doc/bin_prot/Bin_prot/Write/) and [`Bin_prot.Read`](https://ocaml.janestreet.com/ocaml-core/latest/doc/bin_prot/Bin_prot/Read/)). However, it looks as if reading and writing is always to/from a [`Bin_prot.Common`](https://ocaml.janestreet.com/ocaml-core/latest/doc/bin_prot/Bin_prot/Common/)`.buf` buffer (which is a `Bigarray.Array` under the hood).

Nothing in `bin_prot` seems to deal with file I/O. (I haven’t explored every function in every `bin_prot` module, but I’ve checked every module that looks like it might be relevant.) This seems sensible; file I/O can be done elsewhere.

So my guess has been that the intended way to use `bin_prot` is to write or read from a special `bin_prot` buffer to perform serialization conversions. Then one writes the buffer to a file using non-`bin_prot` functions.

However, the type of `bin_prot`'s buffers are specific to `bin_prot`. For example, the fuction `Core.Out_channel.output_buffer` has type `out_channel -> Base__Buffer.t -> unit = <fun>`, but `Base_Buffer.t` is not compatible with `Bin_prot.Common.buf`. I also have not figured out how to write `bin_prot` buffers using I/O functions in `Pervasives`.

What’s a good way to use `bin_prot` to serialize to/from a file? Any help, pointers (“Go read X”), etc. will be appreciated. Thanks.

(Nothing in the `bin_prot` README addresses these questions afaics. I also haven’t yet found answers in either version of RWO.)

---

<div class="post-metadata">

**Author:** ![bcc32](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bcc32/32/203_2.png) [@bcc32](https://discuss.ocaml.org/u/bcc32)\
**Post date:** [May 7, 2018, 5:15am UTC](https://discuss.ocaml.org/t/how-to-write-read-file-using-bin-prot/1961/2 "2018-05-07T05:15:36Z")

</div>

Yeah, I don’t see anything in the README about how to actually do I/O with bin\_prot.

`Bin_prot.Common.buf` is the same type as [`Core_kernel.Bigstring.t`](https://ocaml.janestreet.com/ocaml-core/latest/doc/core_kernel/Core_kernel/Bigstring/#type-t), so you can use, e.g.:

- [`Core.Bigstring.read`](https://ocaml.janestreet.com/ocaml-core/latest/doc/core/Core/Bigstring/#val-read) to read from a file descriptor, or
- [`Core.Bigstring.map_file`](https://ocaml.janestreet.com/ocaml-core/latest/doc/core/Core/Bigstring/#val-map_file) to memory-map a file,

to get file data into/out of a Bigstring.t, after which you can deal with the buffer using bin\_prot.

There is also the [Unpack\_buffer](https://ocaml.janestreet.com/ocaml-core/latest/doc/core_kernel/Core_kernel/Unpack_buffer/) module, which is briefly mentioned in a [comment](https://ocaml.janestreet.com/ocaml-core/latest/doc/bin_prot/Bin_prot/Utils/#val-size_header_length) in Bin\_prot.Utils.

---

<div class="post-metadata">

**Author:** ![mars0i](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mars0i/32/1058_2.png) [@mars0i](https://discuss.ocaml.org/u/mars0i)\
**Post date:** [May 7, 2018, 8:38pm UTC](https://discuss.ocaml.org/t/how-to-write-read-file-using-bin-prot/1961/3 "2018-05-07T20:38:21Z")

</div>

That worked! Thanks @bcc32. `Core.Bigstring` (along with `Core.Unix` for file open/close operations) provide what I need.

I used `Core.Bigstring.write` and `Core.Bigstring.read`, and will try memory mapped files later, too.

(For others reading this later, I’ll note that `Core.Unpack_buffer` mentioned in `Bin_prot.Utils` seems to have moved from `Core` to `Core_kernel`, and doesn’t directly work with regular disk-style files afaics.)
