Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Blockread from file and display items.

Status
Not open for further replies.

Guthro

Technical User
Sep 9, 2006
107
GB
This is a bit of a noobie question but here goes.
I want to use Blockread to find a file and then display its contents. Using the D5 example, it uses an OpenDialog and the SaveDialog. Fine, but how do I display fields from with the file. The output can be a memo, Grid or whatever.
It's been ages since I used Delphi so be gentle :p

What I'm trying to do is read a file that has an inventory of items which are seperated with I believe null characters. It looks much like a flat file. I can now read the first item by reading each single letter from Buf and concatenating it and then assiging it to an editbox text. But whatever I do I can't go beyond the first item.

Is there a simple way to open a file, examine it's contents and then list the English text and ignore all the spurious characters ?

 
If you are reading from a buffer which have zeroes and sending the data to a text-oriented object (like TEdit or TMemo), you need to skip the zeroes.

If you want to keep the line structure in a TMemo, replace the zeroes with #13#10 (if the data lacks them).

Basically, if any char is lesser than #32 (SPACE) and is not #9 (TAB), #13 (CR) or #10 (LF); you need to skip/replace it too.

buho (A).
 
How would I do that ?
Does BlockRead read the whole file including the spaces or does it need code to ignore the unwanted characters at the time of reading ?
 
BlockRead care nothing about characters. It will read everything.


Say you are doing a BlockRead(MyFile, @MyBuffer, 1024) and the file stiil have at least 1024 bytes to read; BlockRead will read exactly 1024 bytes, no more no less, paying no attention to the values, neither skipping nor adding anything. Pure binary read.

Code:
type 
  TMyBuffer = array[1..1024] of char;
var  
  MyBuffer : TMyBuffer;
  i        : integer;

// We have a file opened somewhere and a TMemo ready to be used
// Error checking needed in the read
BlockRead(MyFile, @MyBuffer, 1024);

for i = 1 to 1024 do
  if (MyBuffer[i] < #23) and not (MyBuff[i] in [#9, #10, #13])
    then MyMemo.Text := MyMemo.Text + MyBuff[i];

The code above will get a clean copy of the block in a TMemo, without zeroes or control chars. If the file have not CRLF (#13#10) as delimiters, you will get a continous stream of chars with no line breaks.

To break down lines in a file using zeroes as line delimiters, do something like:

Code:
const
  Filtered = [#1..#23] - [#9, #10, #13];

// Skip is filtered
// Send CRLF if zero
// Send char otherwise
for i = 1 to 1024 do
  if not (MyBuffer[i] in Filtered)
    then if MyBuffer[i] = #0
      then MyMemo.Text := MyMemo.Text + #13#10
      else MyMemo.Text := MyMemo.Text + MyBuff[i];

buho (A).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top