-6
#include "stdafx.h"
#include "Compiler.h"
#include <fstream>

int main() {
    std::ofstream output("file.bin", std::ios::binary | std::ios::trunc);
    if(output.fail()) {
        return 1;
    }
    std::vector<unsigned char> f = Runtime::convert_line_to_instructions("rt_reg str Hello");
    for(unsigned char i : f) {
        //output.write(reinterpret_cast<char*>&i, sizeof(unsigned short)); doesn't work here.
    }
    std::cerr << "Program Compiled!" << std::endl;
    while(true);
    return 0;
}

How do I save an unsigned char vector to a file? I've tried several solutions (including the one that's commented out), but none of them worked.

*convert_line_to_instructions returns an unsigned char vector too.

Teytix
  • 85
  • 1
  • 4
  • 10
  • How would you have saved the data if it were a simple array of `unsigned char`? – PaulMcKenzie Dec 25 '17 at 20:50
  • `sizeof(unsigned short)` why do use short for the size of a char? –  Dec 25 '17 at 20:53
  • @PaulMcKenzie The same really. You can parse arrays with range loops too. – Teytix Dec 25 '17 at 20:54
  • `output << i;` done –  Dec 25 '17 at 20:54
  • @manni66 Doesn't seem to work. File size is still 0 bytes. – Teytix Dec 25 '17 at 20:57
  • @Teytix -- The reason I asked is that if you were successful in using a regular array, using a vector should be little, if any difference. A vector is just a wrapper around a (dynamic) array. The answer you accepted is basically `output.write(array, array_length);` – PaulMcKenzie Dec 25 '17 at 21:17

2 Answers2

4

Use output.put(i); since it takes one character at a time.

But you really want to skip the for-loop and just write the whole vector in one write:

output.write(f.data(), f.size());
Bo R
  • 2,334
  • 1
  • 9
  • 17
0

It's an fstream, so you can use iostream operators, like <<.

#include <fstream>
#include <iostream>
#include <vector>

int main(const int argc, const char* argv[]) {
    std::ofstream output{"file.bin", std::ios::binary | std::ios::trunc};
    if (output.fail())
        return 1;

    std::vector<unsigned char> input = {'r', 't', '_', 'r', 'e', 'g', ' ',
                                        's', 't', 'r', ' ', 'H', 'e', 'l',
                                        'l', 'o'};

    for (const auto i : input)
        output << i;

    return 0;
}

A slightly cleaner approach would be to use an STL algorithm with an iterator:

std::copy(begin(input), end(input), std::ostreambuf_iterator<char>(output));

The ostreambuf_iterator<char> is lightweight, buffered, and performs no formatting.

pestilence669
  • 5,698
  • 1
  • 23
  • 35
  • Your code won't write `binary` even though you opened the file with `std::ios::binary`. https://stackoverflow.com/questions/8277485/writing-binary-to-stdfstream-using-the-operator – Brandon Dec 25 '17 at 21:00
  • @Brandon As listed, it indeed does write binary and I have a hex dump to prove it. The problem is that it isn't guaranteed to write binary data, if you mess with the output formatting. If you don't, then it works. – pestilence669 Dec 25 '17 at 21:21