Read binary file by blocks I wrote a function that allow me to read a binary file using 3 blocks what ever the length of file.

I've decided to divide the length of my file into 3: 


  block 1: ______________
  
  block 2: ______________
  
  block 3: ____


As you can see, this is almost the length of the 3 blocks that I will have using my function.

(What is important for me is to read all characters in my file)

vector&lt; vector&lt;char&gt; &gt;  ReadByBlock(string fileName){

ifstream is (fileName, ifstream::binary);

if (is) {
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);

cout &lt;&lt; "Reading " &lt;&lt; length &lt;&lt; " characters... "&lt;&lt;endl;                                    
const int bloc = 1+length/3;
vector&lt; vector&lt;char&gt; &gt; vect (3,vector&lt;char&gt;(bloc,0));      // create vector of 3 vector of(1+length/3) chars with value 0   


    for(size_t j=0;j&lt;3;++j)
    {
        is.read(&amp;vect[j][0], bloc); 
        for(size_t i=0;i&lt;bloc;++i)  
            cout &lt;&lt; " buf ["&lt;&lt;j&lt;&lt;","&lt;&lt;i&lt;&lt;"]: "&lt;&lt;vect[j][i] &lt;&lt; endl;
    }

 return vect;}}


Because I'm beginner in the world C++ and because I'm thinking about Java while programming my function, I want to know if it is an efficient function that respects the rules of C++ as well the structure of coding using C++.
using namespace std

Since none of the standard library features you use are prefixed by std::, I will assume that you have using namespace std; somewhere. Actually, you should avoid using namespace std; and fully qualify the standard library functions and classes with std:: instead. Your code won't be much longer, and you may avoid potential problems such as name clashes.

Use std::array

One thing that can be noticed is that you know that you will return a collection of exactly three std::vector&lt;char&gt;. You know the size at compile time, therefore you don't need a dynamic container, a fixed-sized one is enough. Let's make that explicit and have your function return an std::array&lt;std::vector&lt;char&gt;, 3&gt; instead.

Check for potential errors

ifstream is (fileName, ifstream::binary); is great. It's how it should be used. However, it may fail, for example if the file fileName does not exist. You should check whether such an error occured before resuming the function:

std::ifstream is (fileName, std::ifstream::binary);
if (not is)
{
    // handle errors (e.g. throw an exception)
}


Note that operator! (here not) is used to check for errors. If it yields false, it means that an error occured when opening the std::ifstream.

Accessing a std::vector underlying array

You used the C++98 way &amp;vec[0] to get the underlying array of an std::vector. Actually, when reading code, knowing what it does can be pretty hard, and that's partly due to the order of evaluation of the operators that may not be obvious for everyone. You can use vec.data() instead. This method was not available for std::vector before C++11, and that's probably why you found the solution &amp;vec[0].