Just an example, why D rocks, and C++ s***s...
Carsten Schlote
carsten.schlote at gmx.net
Wed Mar 16 09:19:19 UTC 2022
Hi,
today I had to write some unittests for the googletest framework.
For one of the tests I had to read binary files, and fed their
contents to some code for testing. In D this is really simple:
```D
import std.file;
auto inbuffer = cast(byte[]) read("MyTestVectorData.bin");
```
In C++ this simple task expands to the following *self-written*
code:
```C++
// Lots of includes needed to compile this code
std::vector<uint8_t> readFile(const char* filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
// Stop eating new lines in binary mode!!!
file.unsetf(std::ios::skipws);
// get its size:
std::streampos fileSize;
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// reserve capacity
std::vector<uint8_t> vec;
vec.reserve(fileSize);
// read the data:
vec.insert(vec.begin(),
std::istream_iterator<uint8_t>(file),
std::istream_iterator<uint8_t>());
return vec;
}
/* ... somewhere else below, then and finally ... */
auto myFilebuffer = readFile("Foo.bin");
```
I have problems to understand, why everything really useful is
not part of STDL. Or why is whitespace skipping some default and
must be unset? Or why is there no simple wrapper to query the
file size instead of doing old-fashion seek-around? Or ...
So, if you need an example, why C++ simply s***s and D just
rocks, use this... ;-)
Regards
Carsten
More information about the Digitalmars-d
mailing list