Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use BlockRead and determine end of file?

Posted on 2009-02-16
3
Medium Priority
?
836 Views
Last Modified: 2013-11-23
I am adapting the Boyer-Moore-Horspool text search algorithm to get the text input from a file. Many of the examples on the net use a string variable rather than a file.  The one example that uses a file does not use the Boyer-Moore-Horspool method.  Arrgguuhh!!!  

To read the text file, I am using a BlockRead.  For efficiency, I want to read the entire text file with one read.  The data is read into an array of char.  I can hard code the array ending value to a huge number like 262,140 ((2**18)-1) or larger which could waste a lot of space.

Is there a way I can quickly calculate the size of the file that is equal to or slightly larger than the exact size of the file?

Please see code below.

Thanks much.
var
 arBuffer : array [0..MaxSize] of char
  F:File;
  BufEnd : integer; 
begin
  AssignFile(F, FileName);
  Reset(F,1);
  BlockRead(F, arBuffer, SizeOf(arBuffer), BufEnd);
  BufPtr := 0;
...
end;

Open in new window

0
Comment
Question by:IT79637
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
dprochownik earned 1000 total points
ID: 23652282
eof(F) will tell you if you are at enf of file.
FileSize(F)  will give you file size
0
 
LVL 4

Expert Comment

by:dprochownik
ID: 23652389
when you are opening file its "cursor" is at the beggining of it. While you are reading data from file, for example by BlockRead procedure, this "cursor" is being moved forward as far as many bytes you have readed.
If there is no more data to read eof(F) will return true value (eof means End Of File).

var
 arBuffer : array [0..1024] of char;
 F:File;
 BufEnd : integer;
 BufPtr: Integer;
begin  
  AssignFile(F, FileName);
  Reset(F,1);
  while not eof(F) do
  begin
    BlockRead(F, arBuffer, SizeOf(arBuffer), BufEnd);
    BufPtr := 0;
    ...
  end;
end;

Open in new window

0
 
LVL 1

Author Closing Comment

by:IT79637
ID: 31547430
Works perfectly.
Thanks much.!
0

Featured Post

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!

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 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