Solved

accessing a text file

Posted on 2000-03-05
9
137 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
Comment Utility
check out the blockread function

GL
Mike
0
 
LVL 7

Accepted Solution

by:
ahalya earned 300 total points
Comment Utility
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
Comment Utility
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
 

Author Comment

by:yegor
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:yegor
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much, Edey and Ahalya! You have saved me hours of despaired search for a solution!!!

yegor
0
 

Author Comment

by:yegor
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now