0

I'm trying to pass the content of a binary file from c++ to node using the node-gyp library. I have a process that creates a binary file using the .fit format and I need to pass the content of the file to js to process it. So, my first aproach was to extract the content of the file in a string and try to pass it to node like this.

   char c;
   std::string content="";
   while (file.get(c)){
    content+=c;
   }

I'm using the following code to pass it to Node

v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(args.GetIsolate(), (void*)content.data(), content.size());
args.GetReturnValue().Set(ab);

In node a get an arrayBuffer but when I print the content to a file it is different to the one that show a c++ cout.

How can I pass the binary data succesfully?

Thanks.

7
  • Is your binary data printable? A lot of binary data values are not printable. Commented Aug 23, 2017 at 20:53
  • Like @Thomas said, how does your binary data look like? Do you use some encoding? Commented Aug 23, 2017 at 20:55
  • You should write content of this buffer to the file instead of printing it. Commented Aug 23, 2017 at 20:56
  • I read the binary data from a binary file with this code: char c; std::string content=""; while (file.get(c)){ content+=c; } I need to pass the content of the file to node, is there any other way to do that? Commented Aug 23, 2017 at 21:05
  • You should open file in binary mode and read entire file content using read method. Though you can do this in node itself. Commented Aug 23, 2017 at 21:11

2 Answers 2

0
  1. Probably the best approach is to write your data to a binary disk file. Write to disk in C++; read from disk in NodeJS.

  2. Very importantly, make sure you specify BINARY MODE.

    For example:

    myFile.open ("data2.bin", ios::out | ios::binary);

  3. Do not use "strings" (at least not unless you want to uuencode). Use buffers. Here is a good example:

How to read binary files byte by byte in Node.js

var fs = require('fs');

fs.open('file.txt', 'r', function(status, fd) {
    if (status) {
        console.log(status.message);
        return;
    }
    var buffer = new Buffer(100);
    fs.read(fd, buffer, 0, 100, 0, function(err, num) {
        ...
    });
});
  1. You might also find these links helpful:

ADDENDUM:

  • The OP clarified that he's considering using C++ as a NodeJS Add-On (not a standalone C++ program.

  • Consequently, using buffers is definitely an option. Here is a good tutorial:

https://community.risingstack.com/using-buffers-node-js-c-plus-plus/

If you choose to go this route, I would DEFINITELY download the example code and play with it first, before implementing buffers in your own application.

Sign up to request clarification or add additional context in comments.

5 Comments

Do you think that passing the buffer from the binary file is a bad idea? Do you think is better to save the file to disk in c++ and read it from node that try to pass the content?
Your Node program and your C++ programs are two different processes, running in two different address spaces, aren't they? If so, then "yes", a disk file is a good choice. Other IPC choices besides "files" might include "sockets" and/or "pipes".
The node-gyp library github.com/nodejs/node-gyp that build bundles for node js with othe code for example c++
Fair enough: Writing a Node Add-on in C++ is also a viable alternative. Please read my post above, and read (and try the sample code) at this link: Scott Frees: Using Buffers to share data between Node.js and C++
I check out you post and base on that I'm working on a example to pass a buffer using this code: info.GetReturnValue().Set(Nan::NewBuffer(&buffer[0], size).ToLocalChecked()); I get data from node, but is not the same that in c++, maybe is because is binary data, I don't know is using this the data is transform o alter in some way. have you try ti use buffer with binary data?
0

It depends but for example using redis

Values can be strings (including binary data) of every kind, for instance you can store a jpeg image inside a value. A value can't be bigger than 512 MB.

If the file is bigger than 512MB, then you can store it in chunks. But I wouldnt suggest since this is an in-memory data store

Its easy to implement in both c++ and node.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.