[C++] Reading a binary file into a byte array

In summary, the conversation discusses the use of a vector of char versus char* or char [] for storing and retrieving blocks of bytes from a binary file. The individual prefers using a vector of char because it simplifies the retrieval process, but acknowledges that there may be some overhead involved. They also mention using a vector in the main code and passing a pointer to old code functions as a workaround. The conversation ends with a question about which means of storage would be better in the given scenario.
  • #1
Silicon Waffle
160
203
I have to read a binary file into a byte array and chop it wherever I like.
I'd like to use a vector of char to do this. But reading some resources from known people, I find they tend to use char* or char []. Using a vector of chars seems easier for retrieval of any block of bytes.
Which means of storage would be better to you ? Thank you.
 
Technology news on Phys.org
  • #2
Silicon Waffle said:
I have to read a binary file into a byte array and chop it wherever I like.
I'd like to use a vector of char to do this. But reading some resources from known people, I find they tend to use char* or char []. Using a vector of chars seems easier for retrieval of any block of bytes.
Which means of storage would be better to you ? Thank you.

When you say "vector", do you mean "array"? If so, your mention of char[] applies...
 
  • #3
I mean C++'s vector class.
 
  • #4
There's a small amount of overhead for a vector, since there's a local structure, that contains a pointer to the allocated memory (as well as other parameters like the object type and number of objects). When combining old and new code, sometimes I'll use a vector in the main code, but call an old code function that takes a pointer to object as a parameter. For example, if I have vector <char> vectorofchar ..., I use &vectorofchar[0] as a parameter for the old code function.
 
  • Like
Likes Silicon Waffle
  • #5


I understand the importance of choosing the most efficient and effective means of storage for data. In this case, I would recommend using a vector of char instead of char* or char[].

A vector of char allows for dynamic resizing, which means you can easily add or remove elements as needed without worrying about the size limitations of a fixed array. This is especially useful when dealing with large binary files.

Additionally, a vector of char allows for easy retrieval of any block of bytes, as you mentioned. This is important for data analysis and manipulation, as it allows for flexibility in accessing specific sections of the file.

Overall, while both char* and char[] may be commonly used for reading binary files, a vector of char offers more advantages in terms of efficiency and ease of use. I would recommend using a vector of char for reading binary files into a byte array and chopping it wherever needed.
 

Related to [C++] Reading a binary file into a byte array

What is a binary file?

A binary file is a type of computer file that stores data in the form of binary code, which is a series of 0s and 1s. This is different from a text file, which stores data in human-readable characters. Binary files are typically used to store data that is not intended to be read or edited by humans, such as images, audio files, or program executables.

How do I read a binary file in C++?

To read a binary file in C++, you can use the ifstream class from the <iostream> header. This class has a member function called read() which allows you to read a specified number of bytes from a file into a byte array. You will need to open the file using the open() member function before reading from it, and remember to close the file using the close() member function when you are done.

What is a byte array?

A byte array, also known as an array of bytes, is a data structure that stores a sequence of bytes in memory. Bytes are the smallest unit of data that a computer can access, and a byte array allows you to manipulate and access these bytes individually. In C++, a byte array is typically represented as an array of char or unsigned char data types.

Can a binary file be read into a byte array in one step?

Yes, it is possible to read a binary file into a byte array in one step using the read() member function of the ifstream class. This function takes in three arguments: a pointer to the byte array, the number of bytes to be read, and the starting position in the file from which to read. By specifying the number of bytes to be read as the size of the byte array, you can read the entire file into the array in one step.

What can I do with a byte array after reading a binary file into it?

Once you have read a binary file into a byte array, you can manipulate the data in the array as needed. For example, you can modify specific bytes in the array, combine multiple arrays, or use the data in the array to perform calculations. You can also write the contents of the byte array to another file or use it to initialize other data structures, such as a bitmap or audio buffer.

Similar threads

  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
2
Replies
57
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
5K
Back
Top