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

Click For Summary

Discussion Overview

The discussion revolves around the best method for reading a binary file into a byte array in C++. Participants explore the use of C++'s vector class versus traditional char pointers or arrays, considering ease of use and performance implications.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using a vector of char for reading a binary file, citing ease of retrieval for any block of bytes.
  • Another participant questions whether "vector" refers to "array," implying a potential misunderstanding of terminology.
  • A participant clarifies that they are referring to C++'s vector class specifically.
  • Concerns are raised about the overhead associated with using a vector, noting that it includes a local structure with a pointer to allocated memory and other parameters.
  • One participant describes a practice of using a vector in main code while passing a pointer to the vector's data to older code functions.

Areas of Agreement / Disagreement

Participants express differing views on the advantages and disadvantages of using a vector versus char pointers or arrays, indicating that there is no consensus on the best approach.

Contextual Notes

Participants do not fully resolve the implications of overhead when using vectors, nor do they clarify the specific contexts in which one method may be preferred over another.

Silicon Waffle
Messages
160
Reaction score
202
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
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...
 
I mean C++'s vector class.
 
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   Reactions: Silicon Waffle

Similar threads

  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 57 ·
2
Replies
57
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
7K