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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a fixed width tect file

Status
Not open for further replies.

aagustin

Programmer
Jul 23, 2001
12
US
Howdy,

I'm rewriting an application, currently it reads a one column text file and takes the value of the value read in the first column and we to a typetext() function to it. Now we need to take a new text file and loop through it, but this time with two columns:

COLA COLB
123456789 99999

I need to know how to read two columns, hold it in memory and do a typetext for both...

Any help is appreciated.

 
I'm not sure what TypeText is.

To read a fixed width file without cr/lf's you need to make a variable of type file, and use blockread and blockwrite functions to read the records. If you've got cr/lf's it's easier to use a text file and readln. Once you've read a block in, you can use the copy function to copy the first column and second column from the record/string.

TealWren
 
Hiya,

So, when using ReadLn(str) it will
contain '123456789 99999' in the case you described.

Now, you can consider the spaces between the numbers as separator.

The following procedure splits the string into two parts:
Code:
procedure TForm1.split(txt: string);
var
 i:integer;
 part1 : string;
 part2 : string;
begin
 i:=1;
 part1:='';
 part2:='';
 while (i <= Length(txt)) and (txt[i] <> ' ') do
 begin
  part1 := part1 + txt[i];
  i:=i+1;
 end;
 if i < Length(txt) then
 begin
  // loops until there are no more spaces to be found
  while txt[i] = ' ' do
   i:=i+1;
  // loops true the string starting from first character
  // after last space
  while i <= Length(txt) do
  begin
   part2 := part2 + txt[i];
   i:=i+1;
  end;
 end;
 ShowMessage('Col1: '+part1 + ' ' + 'Col2: '+part2);
end;
If either of the columns can contain spaces as text, using a space as separator wont do. In that case, you will have to use another char as separator, eg:

123456789#99999

In that case, you alter the first while loop like this:

Code:
while (i <= Length(txt)) and (txt[i] <> '#') do

and remove the while loop that checks for unnecessary spaces:

Code:
while txt[i] = ' ' do
 i:=i+1;

This is just an idea I had, don't know if it will serve your needs, sure hope it does :p

Gtz,

Kristof
 
Thanks, for the info and help. I'll try this out...It maybe exactly what I need. I'll let you know.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top