Question: I am reading strings from a structure in a file where each string has a fixed length, with `'\0'` padding. They are not zero-terminated if the stored string needs the whole length.

I'm currently constructing `std::string`s out of those like this:

```
// char MyString[1000];
std::string stdmystring(MyString, ARRAYSIZE(MyString));

```

However, this copies the padding, too. I could trim the string now, but is there an elegant and quick way to prevent the copying in the first place?

Speed is more important than space, because this runs in a loop.

Answer: 
Well If size is not a problem one potential way to do it is to create an empty `std::string` then use `reserve()` to pre-allocate the space potentially needed and then add each char until you come across `'\0'`.

```
std::string stdmystring;
stdmystring.reserve(MyString_MAX_SIZE) ;
for(size_t i=0;i<MyString_MAX_SIZE && MyString[i]!='\0';++i);
stdmystring+=MyString[i];

```

`reserve()` garanties you one memory allocation since you know the max\_size and the string will never get larger than that.

The calls to += operator function will probably be inlined but it still has to check that the string has the needed capacity which is wasteful in your case. Infact this could be the same or worse than simply using strlen to find the exact length of the string first so you have to test it.
I think the most straightforward way is to overallocate your internal `MyString` array by one byte, always null terminate that final byte, and use the C-string constructor of `std::string`. (Keep in mind that most likely your process will be I/O bound on the file so whatever algorithm the C-string constructor uses should be fine).