3

I am looking for a solution to the same problem in the question What is the best way to read an entire file into a std::string in C++? and looking at the dates being way back in the mists of 2008.

Is there a better C++11/C++17 solution for this problem?

That is with 'better' being defined in any way you'd care. More space/time efficient, more readable, a shorter one-liner. Justifying why your solution is better would be appreciated

(C++11 is preferred for my problem, but I don't want to have the same issue as the previous question too soon, C++17 is welcomed happily.)

Community
  • 1
  • 1
Yann
  • 948
  • 2
  • 12
  • 28
  • 2
    What is wrong in the answer to the other question? What kind of improvement do you seek? "better" is not very well defined. IMHO, it would be hard to be faster, and for readability.. it's a one-liner two with a comment. – luk32 Jan 27 '17 at 17:55
  • 1
    @luk32 Like I said, I don't think there's necessarily anything wrong with any of the answers there, and if the answer to my question winds out being "No, we've not made any improvements in the last 9 years to this particular problem", I'll happily accept that as the answer. But, with lambdas, new std functions etc, I felt it was likely there was a pithier solution now. – Yann Jan 27 '17 at 17:57
  • 1
    I mean, I'm really not sure how you're going to improve on [directly constructing a string from a pair of stream iterators](http://stackoverflow.com/a/116177/4892076) for concision, especially when constructing one of them can be done with `{}`. `std::string str(std::istreambuf_iterator{ifs}, {});` is pretty short, legible, and fast... – jaggedSpire Jan 27 '17 at 18:00
  • @Yann One of the answers in the link you posted _is_ for C++17. –  Jan 27 '17 at 18:04
  • @NeilButterworth None for C++11, which I admit was the primary motivation for this particular question as it's what I'm using, like I said in the question. – Yann Jan 27 '17 at 18:08
  • 6
    Potentially out-of-date questions are a moral quandary around here. I doubt you'll find a consensus on how to handle this. The original model wants you to offer a bounty on the original question. – Lightness Races in Orbit Jan 27 '17 at 18:20
  • The problem isn't most efficient way to read a file into a string, it's determining what encoding is used by said octets. – Chad Mar 04 '17 at 06:06

0 Answers0