Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Dec 16, 2014 #1
    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.
  2. jcsd
  3. Dec 16, 2014 #2


    User Avatar

    Staff: Mentor

    When you say "vector", do you mean "array"? If so, your mention of char[] applies...
  4. Dec 16, 2014 #3
    I mean C++'s vector class.
  5. Dec 16, 2014 #4


    User Avatar
    Homework Helper

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook