C++ read the whole file in buffer [duplicate]

There’s no need for wrapper classes for very basic functionality: std::ifstream file(“myfile”, std::ios::binary | std::ios::ate); std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); std::vector<char> buffer(size); if (file.read(buffer.data(), size)) { /* worked! */ }

Understanding the meaning of the term and the concept – RAII (Resource Acquisition is Initialization)

So why isn’t that called “using the stack to trigger cleanup” (UTSTTC:)? RAII is telling you what to do: Acquire your resource in a constructor! I would add: one resource, one constructor. UTSTTC is just one application of that, RAII is much more. Resource Management sucks. Here, resource is anything that needs cleanup after use. … Read more

Does C++ support ‘finally’ blocks? (And what’s this ‘RAII’ I keep hearing about?)

No, C++ does not support ‘finally’ blocks. The reason is that C++ instead supports RAII: “Resource Acquisition Is Initialization” — a poor nameā€  for a really useful concept. The idea is that an object’s destructor is responsible for freeing resources. When the object has automatic storage duration, the object’s destructor will be called when the … Read more

Why is it wrong to use std::auto_ptr with standard containers?

The C++ Standard says that an STL element must be “copy-constructible” and “assignable.” In other words, an element must be able to be assigned or copied and the two elements are logically independent. std::auto_ptr does not fulfill this requirement. Take for example this code: class X { }; std::vector<std::auto_ptr<X> > vecX; vecX.push_back(new X); std::auto_ptr<X> pX … Read more