Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The question is exceedingly unclear. How do you determine the specific line? If it is the nth line, simplest solution is just to call <code>getline</code> n times, throwing out all but the last results; calling <code>ignore</code> n-1 times might be slightly faster, but I suspect that if you're always reading into the same string (rather than constructing a new one each time), the difference in time won't be enormous. If you have some other criteria, and the file is really big (which from your description it isn't) and sorted, you might try using a binary search, seeking to the middle of the file, reading enough ahead to find the start of the next line, then deciding the next step according to it's value. (I've used this to find relevant entries in log files. But we're talking about files which are several Gigabytes in size.)</p> <p>If you're willing to use system dependent code, it might be advantageous to memory map the file, then search for the nth occurance of a '\n' (<code>std::find</code> n times).</p> <p>ADDED: Just some quick benchmarks. On my Linux box, getting the 100000th word from <code>/usr/share/dict/words</code> (479623 words, one per line, on my machine), takes about </p> <ul> <li>272 milliseconds, reading all words into an <code>std::vector</code>, then indexing,</li> <li>256 milliseconds doing the same, but with <code>std::deque</code>,</li> <li>30 milliseconds using <code>getline</code>, but just ignoring the results until the one I'm interested in,</li> <li>20 milliseconds using <code>istream::ignore</code>, and</li> <li>6 milliseconds using <code>mmap</code> and looping on <code>std::find</code>.</li> </ul> <p>FWIW, the code in each case is:</p> <p>For the std:: containers:</p> <pre><code>template&lt;typename Container&gt; void Using&lt;Container&gt;::operator()() { std::ifstream input( m_filename.c_str() ); if ( !input ) Gabi::ProgramManagement::fatal() &lt;&lt; "Could not open " &lt;&lt; m_filename; Container().swap( m_words ); std::copy( std::istream_iterator&lt;Line&gt;( input ), std::istream_iterator&lt;Line&gt;(), std::back_inserter( m_words ) ); if ( static_cast&lt;int&gt;( m_words.size() ) &lt; m_target ) Gabi::ProgramManagement::fatal() &lt;&lt; "Not enough words, had " &lt;&lt; m_words.size() &lt;&lt; ", wanted at least " &lt;&lt; m_target; m_result = m_words[ m_target ]; } </code></pre> <p>For <code>getline</code> without saving:</p> <pre><code>void UsingReadAndIgnore::operator()() { std::ifstream input( m_filename.c_str() ); if ( !input ) Gabi::ProgramManagement::fatal() &lt;&lt; "Could not open " &lt;&lt; m_filename; std::string dummy; for ( int count = m_target; count &gt; 0; -- count ) std::getline( input, dummy ); std::getline( input, m_result ); } </code></pre> <p>For <code>ignore</code>:</p> <pre><code>void UsingIgnore::operator()() { std::ifstream input( m_filename.c_str() ); if ( !input ) Gabi::ProgramManagement::fatal() &lt;&lt; "Could not open " &lt;&lt; m_filename; for ( int count = m_target; count &gt; 0; -- count ) input.ignore( INT_MAX, '\n' ); std::getline( input, m_result ); } </code></pre> <p>And for <code>mmap</code>:</p> <pre><code>void UsingMMap::operator()() { int input = ::open( m_filename.c_str(), O_RDONLY ); if ( input &lt; 0 ) Gabi::ProgramManagement::fatal() &lt;&lt; "Could not open " &lt;&lt; m_filename; struct ::stat infos; if ( ::fstat( input, &amp;infos ) != 0 ) Gabi::ProgramManagement::fatal() &lt;&lt; "Could not stat " &lt;&lt; m_filename; char* base = (char*)::mmap( NULL, infos.st_size, PROT_READ, MAP_PRIVATE, input, 0 ); if ( base == MAP_FAILED ) Gabi::ProgramManagement::fatal() &lt;&lt; "Could not mmap " &lt;&lt; m_filename; char const* end = base + infos.st_size; char const* curr = base; char const* next = std::find( curr, end, '\n' ); for ( int count = m_target; count &gt; 0 &amp;&amp; curr != end; -- count ) { curr = next + 1; next = std::find( curr, end, '\n' ); } m_result = std::string( curr, next ); ::munmap( base, infos.st_size ); } </code></pre> <p>In each case, the code is run</p>
    singulars
    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. This table or related slice is empty.
    1. 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