Solved

accessing a text file

Posted on 2000-03-05
9
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 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