The problem with text files and line is that the lines have different length, so it could be considered as a "jagged array" of arrays of characters. You can never predict the position of last line if you did not scan all the file.
If the file is big, the only work-around I can see is this: at least once scan the input file from the beginning to the end using
System.IO.StreamReader.ReadLine
,
http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx[
^]. Throw out the line contents, only remember position in the file and (optionally) the length of each line. Put this information in some array which will play a role of the file map. If this data is too big even for such a map, write this data in a binary file. As the file is binary, each record takes the same size, so you can use random-access read of this file. This way, reading a line from an original input file will be two-step: first, read the map record at required index. For this purpose, set
Stream.Position
to
sizeof(metadataRecord) + index * sizeof(mapRecord)
, where
metadataRecord
could contain some fixed-size meta-data (for example, total number of lines), and
mapRecord
is the record about position/size of each line in the input file. On a second step, set
Position
in the input file from
mapRecord
and read from this point. I would advise to keep the input file open in binary mode. Use the class
System.IO.BinaryReader
and its property
BaseStream
needed to work with
Stream.Position
,
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[
^].
—SA