While string
should be used for working with strings, I would like to know what structure you should use in C++ when working with blocks of data.
I'm asking this because it would be nicer to use one parameter instead of passing a char* data
and size_t size
(or a custom structure).
Binary fileIt contains 1's and 0's, which are easily understood by computers. The error in a binary file corrupts the file and is not easy to detect. In binary file, the integer value 1245 will occupy 2 bytes in memory and in file. A binary file always needs a matching software to read or write it.
Read, write and seek operations can be performed on binary files with the help of fread(), fwrite() and fseek() functions, respectively. After reading or writing a structure, the file pointer is moved to the next structure. The fseek() function can move the pointer to the position as requested.
Binary data is a type of data that is represented or displayed in the binary numeral system. Binary data is the only category of data that can be directly understood and executed by a computer. It is numerically represented by a combination of zeros and ones.
int is the integer data type. Integers are represented in binary format. Binary numbers may be stored in either 1's or 2's complement form. Most machines use 2' complement.
std::vector<unsigned char>
or, preferably,
std::vector<std::uint8_t>
(In C++11, uint8_t
can be found in <cinttypes>
. Older compilers, but not MSVC, may have the C99 header <inttypes.h>
. If your data is a sequence of 16-bit units, use uint16_t
etc.)
If the size of the data blocks is known at compile time, std::array
is appropriate; it wastes less space than vector
.
There are a number of containers in the STL, not only vector
. Look and choose what fits your situation.
The above solutions is good, but consider this solution may be good :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With