Eduardo Lucio Eduardo Lucio - 1 year ago 74
C++ Question

What is the most suitable type of vector to keep the bytes of a file?

What is the most suitable type of vector to keep the bytes of a file?

I'm considering using the int type, because the bits "00000000" (1 byte) are interpreted to 0!

The goal is to save this data (bytes) to a file and retrieve from this file later.

NOTE: The files contain null bytes ("00000000" in bits)!

I'm a bit lost here. Help me! =D Thanks!


To read the file I'm using this function:

char* readFileBytes(const char *name){
std::ifstream fl(name);
fl.seekg( 0, std::ios::end );
size_t len = fl.tellg();
char *ret = new char[len];
fl.seekg(0, std::ios::beg);, len);
return ret;

NOTE I: I need to find a way to ensure that bits "00000000" can be recovered from the file!

NOTE II: Any suggestions for a safe way to save those bits "00000000" to a file?

NOTE III: When using char array I had problems converting bits "00000000" for that type.

Code Snippet:

int bit8Array[] = {0, 0, 0, 0, 0, 0, 0, 0};
char charByte = (bit8Array[7] ) |
(bit8Array[6] << 1) |
(bit8Array[5] << 2) |
(bit8Array[4] << 3) |
(bit8Array[3] << 4) |
(bit8Array[2] << 5) |
(bit8Array[1] << 6) |
(bit8Array[0] << 7);


Following the @chqrlie recommendations.

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <random>
#include <cstring>
#include <iterator>

std::vector<unsigned char> readFileBytes(const char* filename)
// Open the file.
std::ifstream file(filename, std::ios::binary);

// Stop eating new lines in binary mode!

// Get its size
std::streampos fileSize;

file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);

// Reserve capacity.
std::vector<unsigned char> unsignedCharVec;

// Read the data.
std::istream_iterator<unsigned char>(file),
std::istream_iterator<unsigned char>());

return unsignedCharVec;

int main(){

std::vector<unsigned char> unsignedCharVec;

// txt file contents "xz"

// Letters -> UTF8/HEX -> bits!
// x -> 78 -> 0111 1000
// z -> 7a -> 0111 1010

for(unsigned char c : unsignedCharVec){
printf("%c\n", c);
for(int o=7; o >= 0; o--){
printf("%i", ((c >> o) & 1));
printf("%s", "\n");

// Prints...
// x
// 01111000
// z
// 01111010

return 0;


This is the code I am using using to write to a binary file:

std::ofstream file("max.bin", std::ios::out|std::ios::binary);
file.write(fileBytesOutput.size() ? (char*)&fileBytesOutput[0] : 0, std::streamsize(fileBytesOutput.size()));


Definitely the solution to the problem of the "00000000" bits (1 byte) was to change the type that stores the bytes of the file to "std::vector" as the guidance of friends. "std::vector" is a universal type (exists in all environments) and will accept any octal (unlike char* in "UPDATE I")!

In addition, changing from array (char) to vector (unsigned char) was crucial for success! With Vector I manipulate my data more securely and completely independent of its content (in char array I have problems with this).

Thanka a lot!

Answer Source

There are 3 problems in your code:

  • You use the char type and return a char *. Yet the return value is not a proper C string as you do not allocate an extra byte for the '\0' terminator nor null terminate it.

  • If the file may contain null bytes, you should probably use type unsigned char or uint8_t to make it explicit that the array does not contain text.

  • You do not return the array size to the caller. The caller has no way to tell how long the array is. You should probably use a std::vector<uint8_t> or std::vector<unsigned char> instead of an array allocated with new.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download