Just an example, why D rocks, and C++ s***s...

user1234 user1234 at 12.de
Wed Mar 16 11:20:45 UTC 2022


On Wednesday, 16 March 2022 at 09:19:19 UTC, Carsten Schlote 
wrote:
> 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:
>
> [...]
>
> So, if you need an example, why C++ simply s***s and D just 
> rocks, use this... ;-)
>
>     Regards
>       Carsten

You're right on the fact that D std is better for that but 
apparently there were shorter c++ alternatives (just searched 
"stdc++ read file in vector")

```C++
std::vector<uint8_t> vec readFile(const char* filename)
{
   std::vector<uint8_t> vec;
   if (FILE *fp = fopen("MyTestVectorData.bin", "r"))
   {
     char buf[1024];
     while (size_t len = fread(buf, 1, sizeof(buf), fp))
     {
       v.insert(vec.end(), buf, buf + len);
     }
     fclose(fp);
   }
   return vec;
}
```

or

```C++
std::vector<uint8_t> readFile(std::string filename)
{
   std::ifstream instream(filename, std::ios::in | 
std::ios::binary);
   std::vector<uint8_t> 
data((std::istreambuf_iterator<char>(instream)),
      std::istreambuf_iterator<char>());
   return data;
}
```


More information about the Digitalmars-d mailing list