Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seek for TextFile

I'm working with very large text files, 2GB and more. I would like to have a Seek() like function. Has anyone done something like that? Loading to TStringList is out of the question. Also working with untyped file as well. For now I'm using readLn, but that lasts too long. Thanks.

like image 452
Mihaela Avatar asked Jan 17 '11 20:01

Mihaela


People also ask

What is the use of SEEK () in files?

The seek() method sets the current file position in a file stream.

Does SEEK () work for binary files?

Example 2: Seek() function with negative offset only works when file is opened in binary mode.

What is the use of seek () and tell () in file handling?

The tell() method returns the current file position in a file stream. Tip: You can change the current file position with the seek() method.

What is the syntax for SEEK () method?

Python File seek() Method Python file method seek() sets the file's current position at the offset. The whence argument is optional and defaults to 0, which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file's end.


2 Answers

Map the file into memory (CreateFileMapping/MapViewOfFile) by pieces, then scan the mapped memory and build an index - the list of positions of each line beginnings. Then your seek operation will be performed by getting position of Nth line in the file and seeking to this position. Use TFileStream then to perform random access to the file or, if you only read the file, you can use file mappings for random access as well - this might be even faster than using TFileStream in parallel to file mapping.

like image 168
Eugene Mayevski 'Callback Avatar answered Oct 08 '22 03:10

Eugene Mayevski 'Callback


Try GpHugeFile.

Encapsulation of Windows file-handling routines that allows work with >2GB files.

Included is support for non-buffered access (FILE_FLAG_NO_BUFFERING) and buffering for sequentially accessed files. Includes also stream wrapper class.

like image 3
DiGi Avatar answered Oct 08 '22 02:10

DiGi