snippetcppMinor
Read binary file by blocks
Viewed 0 times
blocksfilebinaryread
Problem
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)
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++.
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 > 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 > vect (3,vector(bloc,0)); // create vector of 3 vector of(1+length/3) chars with value 0
for(size_t j=0;j<3;++j)
{
is.read(&vect[j][0], bloc);
for(size_t i=0;i<bloc;++i)
cout << " buf ["<<j<<","<<i<<"]: "<<vect[j][i] << 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++.
Solution
using namespace stdSince 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::arrayOne thing that can be noticed is that you know that you will return a collection of exactly three
std::vector. 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, 3> 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 arrayYou used the C++98 way
&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 &vec[0].Code Snippets
std::ifstream is (fileName, std::ifstream::binary);
if (not is)
{
// handle errors (e.g. throw an exception)
}Context
StackExchange Code Review Q#63777, answer score: 6
Revisions (0)
No revisions yet.