Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure that there's a <strong>best</strong> way, but using the two iterator constructor would be more idiomatic:</p> <pre><code>FileContainer container( (std::istreambuf_iterator&lt;char&gt;( testFile )), (std::istreambuf_iterator&lt;char&gt;()) ); </code></pre> <p>(I notice that you have the extra parentheses in your <code>assign</code>. They aren't necessary there, but they are when you use the constructor.)</p> <p>With regards to performance, it would be more efficient to pre-allocate the data, something like:</p> <pre><code>FileContainer container( actualSizeOfFile ); std::copy( std::istreambuf_iterator&lt;char&gt;( testFile ), std::istreambuf_iterator&lt;char&gt;(), container.begin() ); </code></pre> <p>This is slightly dangerous; if your estimation is too small, you'll encounter undefined behavior. To avoid this, you could also do:</p> <pre><code>FileContainer container; container.reserve( estimatedSizeOfFile ); container.insert( container.begin(), std::istreambuf_iterator&lt;char&gt;( testFile ), std::istreambuf_iterator&lt;char&gt;() ); </code></pre> <p>Which of these two is faster will depend on the implementation; the last time I measured (with g++), the first was slightly faster, but if you're actually reading from file, the difference probably isn't measurable.</p> <p>The problem with these two methods is that, despite other answers, there is no portable way of finding the file size other than by actually reading the file. Non-portable methods exist for some systems (<code>fstat</code> under Unix), but on other systems, like Windows, there is <strong>no</strong> means of finding the exact number of <code>char</code> you can read from a text file. And of course, there's no guarantee that the results of <code>tellg()</code> will even convert to an integral type, and that if it does, that they won't be a magic cookie, with no numerical signification.</p> <p>Having said that, in practice, the use of <code>tellg()</code> suggested by other posters will often be "portable enough" (Windows and most Unix, at least), and the results will often be "close enough"; they'll usually be a little too high under Windows (since the results will count the carriage return characters which won't be read), but in a lot of cases, that's not a big problem. In the end, it's up to you to decide what your requirements are with regards to portability and precision of the size.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload