c++11 - C++ Read only random lines in a file -


i had requirement read text file large decide read lines in file. can use seek method jump given line? can read line because text file large reading whole file wasting lot of time. if not possible, 1 give better solution that? (seek given line , read it) (i know binary text files reading byte byte)

ex of file

event1 0  subevent 1  subevent 2  event2  3  

(in file after 1 event display number of lines want seek previous event)

yes, can seek point in file read there. 1 possible problem if lines different lengths, random location in file have higher probability of being in longer line: you're not getting evenly distributed probabilities of different lines. if really must have identical probabilities need make @ least 1 pass on file find start of each line - can store offsets in vector , randomly select vector element guide seeking line data in file. if care little bit, can perhaps advance small random number of lines past 1 seek to... odds bit, avoids initial pass, isn't perfect. hansmaad's comment adds neat approach - perfect results pretty-good performance - requires have lines numbered in file itself.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -