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?

IT79637
IT79637 asked
on
Medium Priority
846 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

Comment
Watch Question

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.

Ask the Experts
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

Author

Commented:
Works perfectly.
Thanks much.!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

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

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.