Solved

accessing a text file

Posted on 2000-03-05
9
142 Views
Last Modified: 2010-04-04
Hello!

I badly need to access strings in a text file WITHOUT reading the whole file, because it is HUGE (5 to 10 Mb!). What I need is to be able to read QUICKLY, say, the lines 4567 and 4568 and to dislay them in a RichEdit. The lines are relative ly short (300 characters or so, but more than 256). Since the file is always the same (it is not edited), it is possible to create a table with all the lines' offsets from the beginning of the file, but I do not know how to do that either, nor how to read the particular lines if I know the offsets... PLease help!

Thank you very much in advance,

yegor

P.S. A piece of working code would be GREATLY AND THANKFULLY appreciated! I am not a professional.
0
Comment
Question by:yegor
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2586074
check out the blockread function

GL
Mike
0
 
LVL 7

Accepted Solution

by:
ahalya earned 300 total points
ID: 2586162
In the following code, i'm assuming that you have the saved the offsets of all lines in an array called LinePos[Line#].

We open the file as byte, and then use the "Seek" command to locate the pointer correctly at the start of the line. Then we read each byte until we reach the end of the line.

//============================
var LinePos : array[1..maxlines] of Cardinal;
    f : file of byte;

begin;
AssignFile(f, s);
Reset(f); s := '';

// example Seeking halfway into file...
Seek(f,  FileSize(f) div 2);

//Seeking to Line 100 ...
Seek(f, LinePos[100]);
repeat
  read(f, c);
  s := s + chr(c);
until c = 13;
//===============================

Instead of using an array you can use an INI file to save & retrive the offsets to each of the lines (or a DB).

suppose if your INI file looks like,
[LineOffsets]
1=0
2=240
3=600
4=650
.....

then you'd read the offset simply using
  LinePos := INI.ReadInteger('LineOffsets, IntToStr(Line#), Default);

(Note that INIs have a limit of 64kb)
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2586176
Just in case all your lines are of EQUAL LENGTH, then it'll be lot easier.  
In that case you can call reset with the "Record Size" (each of your string is a record)


var f : file; //no "of byte" now.

reset(f, RecordSize);
Seek(Line#);
Read(f, record);
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:yegor
ID: 2586219
Ahalya,

thank you very much for your quick reply. Among other things, you wrote:

In the following code, i'm assuming that you have the
                    saved the offsets of all lines in an array called
                    LinePos[Line#].

How do I do that?

Thank you,

yegor
0
 

Author Comment

by:yegor
ID: 2586228
Ahalya,

thank you very much for your quick reply. Among other things, you wrote:

In the following code, i'm assuming that you have the
                    saved the offsets of all lines in an array called
                    LinePos[Line#].

How do I do that?

Thank you,

yegor
0
 
LVL 6

Expert Comment

by:edey
ID: 2586247
If you know (ahead of time) how long each line is, then as Ahalya suggested, save these lengths in an array (or ini file) if you don't, then you should (as mentioned above) blockread a chunk of data, search the chunk for eol's and copy lines one at a time to the memo.


GL
Mike
0
 

Author Comment

by:yegor
ID: 2586271
Thank you very much, Edey and Ahalya! You have saved me hours of despaired search for a solution!!!

yegor
0
 

Author Comment

by:yegor
ID: 2586277
Thank you very much, Edey and Ahalya! You have saved me hours of despaired search for a solution!!!

yegor
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2588458
yegor,

I am not sure whether you have solved problems or not. If you need any more info about this Q., please let us know.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree add node to another virtuailstring tree list 4 115
error 1.1 400 Bad request idhttp delphi 18 92
find a node in VST 2 69
enhance the following code 3 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question