We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# How to use BlockRead and determine end of file?

on
Medium Priority
846 Views
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?

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

## View Solution Only

Commented:
eof(F) will tell you if you are at enf of file.
FileSize(F)  will give you file size

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
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
BufPtr := 0;
...
end;
end;
``````

Commented:
Works perfectly.
Thanks much.!
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile