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

  • #1
156
203

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
berkeman
Mentor
56,895
6,864
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
156
203
I mean C++'s vector class.
 
  • #4
rcgldr
Homework Helper
8,681
514
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.
 

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

  • Last Post
2
Replies
33
Views
2K
Replies
11
Views
30K
Replies
8
Views
4K
Replies
4
Views
10K
Replies
4
Views
3K
Replies
1
Views
15K
Replies
6
Views
3K
Replies
3
Views
4K
  • Last Post
Replies
11
Views
6K
Top