?
Solved

accessing a text file

Posted on 2000-03-05
9
Medium Priority
?
147 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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