?
Solved

accessing a text file

Posted on 2000-03-05
9
Medium Priority
?
149 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 1200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 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