Thanks,
Brian
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
| Brian Milburn bmil...@solidoak.com |
| Solid Oak Software CIS: 74774,551 - GO SOLIDOAK |
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Read the file as a non typed file. Use BlockRead to read in
chuncks of the file at a time, counting the number of carriage
returns in each block.
If you are using Windows, another option may be to use the
_lread() function.
I hope this at least gets you started.
Best regards,
Michael Vincze
m...@asd470.dsg.ti.com
>Does anyone know a *fast* way to determine the number of lines in an
>ASCII text file?
>
>Thanks,
>
>Brian
>*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
>| Brian Milburn bmil...@solidoak.com |
>| Solid Oak Software CIS: 74774,551 - GO SOLIDOAK |
>*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
>
>
The Only way I know is the following:
var Count: Longint;
InFile: Text;
begin
Assign(Infile,'Somefile.txt');
reset(Infile); {Not used in standard pascal, only in Turbo Pascal}
Count:=0;
While not eof(Infile) do begin
readln(Infile);
count:=count+1;
end;
close(Infile);
end.
Count would be the number of lines and the file would need to be closed
at the end of the program.
Hope it helps.
--
o__ o__ o__ o__ o__
_.>/)_ _.>/)_ _.>/)_ _.>/)_ _.>/)_ Cya
(_) \(_) (_) \(_) (_) \(_) (_) \(_) (_) \(_)
- Andres, an...@lafn.org
Sorry to be blunt, but... NO!
You have to
While Not EoF (Text_File) do
Begin
Inc (Counter); Readln (Text_File)
End;
Put this in: Function (File_Name): Word;
James Richard (JRIC...@TUNS.CA)
"No Assembler Required!"
>>Does anyone know a *fast* way to determine the number of lines in an
>>ASCII text file?
>Sorry to be blunt, but... NO!
>You have to
>While Not EoF (Text_File) do
>Begin
> Inc (Counter); Readln (Text_File)
>End;
>Put this in: Function (File_Name): Word;
But you can increase the buffer size for reading (see help for TextRec) to
increase the speed. I don't know exactly how to do this, but there must be a
solution.
--
Henrik Haftmann henrik....@e-technik.tu-chemnitz.de
>Does anyone know a *fast* way to determine the number of lines in an
>ASCII text file?
Open the file as a binary file and look for the #13 character. This way
you can use Blockread to read in large chunks of your file at a time.
You could then close and reopen it later as a text file if you want to
be able to take advantage of the Pascal's text file Reading capabilities.
For example,
program Linecount;
const
MAXCHUNK = 4096; { Larger probably gains little additional speed. }
Carriage_Return = 13;
type
Chunk = Array[0..PRED(MAXCHUNK)] of byte;
var
Count : Word;
BytesRead : Word;
i : Word;
p : ^Chunk;
fp : File;
begin
Count := 0;
new(p);
Assign(fp,'Filename.txt');
reset(fp,1);
repeat
BlockRead(fp, p^, MAXCHUNK, BytesRead);
for i := 0 to PRED(BytesRead) do
if p^[i] = Carriage_Return then
INC(Count);
until eof(fp);
close(fp);
dispose(p);
Writeln('Lines in file = ', Count);
end.
Obviously this isn't REALLY fast, but it's faster than using
while not eof(fp)
begin
readln;
inc(Count);
end;
If you want to increase the speed, recode the character
comparison in BASM.
Douglas P. Webb | Life is a Game
CETAC Technologies Inc. | The object of which is . . .
dw...@cetac.com 402-733-2829 | To find out the Object of the Game
Views expressed here are mine and do NOT reflect CETAC company policy.
>Does anyone know a *fast* way to determine the number of lines in an
>ASCII text file?
if you mean a tool ...
grep -roc $ <filename>
Suresh
-- Suresh <dha...@kalimat.pc.my>
"Seize thy chance, for it shall come to thee no more"
: >Sorry to be blunt, but... NO!
: >You have to
: >While Not EoF (Text_File) do
: >Begin
: > Inc (Counter); Readln (Text_File)
: >End;
: >Put this in: Function (File_Name): Word;
A faster way would be to use BlockRead() to pull large chunks into a
buffer, and then scan the buffer for CR+LF - something like this (very
rough and untested pseudo code);
const
marker=^M;
type
buf=array[0..65000] of char; {use zero based for pchar compatibility}
var
buffer : ^buf;
lines : longint;
bin : file;
begin
assign and open (bin);
allocate mem for buffer;
lines := 0;
while not eof(bin) do
begin
BlockRead(bin, buffer^, sizeof(buffer^), .....);
scan through (buffer^) for marker;
inc (lines) on each occurence;
end;
close (bin);
free mem for buffer;
return lines;
end;
You can of course use other/additional methods for checking if the end of
the file has been reached.
The Chief
---------
Dr. Abimbola A. Olowofoyeku (The African Chief)
E-mail : la...@keele.ac.uk
Author of:
1. Chief's Installer Pro 1.70 for Windows:
winner of PC PLUS Magazine Gold Award (April 1995 U.K. edition) **
ftp://ftp.demon.co.uk/pub/ibmpc/windows/chief/pro/cinstp17.zip
2. Windows Command Line and Batch Processor (WCL) v7.80:
ftp://ftp.demon.co.uk/pub/ibmpc/windows/wcl/wincmd78.zip
>James Richard writes:
>>>Does anyone know a *fast* way to determine the number of lines in an
>>>ASCII text file?
>But you can increase the buffer size for reading (see help for TextRec) to
>increase the speed. I don't know exactly how to do this, but there must be a
>solution.
You can find pascal source for increasing the buffer of text files in
ftp.uni-freiburg.de:/pub/pc/msdos/turbopas/txtbuf.pas
There's a demo included in the file txbufdem.pas
Klaus
--
Klaus Hartnegg, Arbeitsgruppe Hirnforschung, Institut fuer Biophysik,
Hansa-Strasse 9a, D-79104 Freiburg, Tel +49 761 270 9516, Fax 9540
OUR TELEPHONE NUMBERS CHANGE ON 1-JUN FROM 270-xxxx TO 203-xxxx !!
>>James Richard writes:
>>Does anyone know a *fast* way to determine the number of lines in an
>>ASCII text file?
>For example,
>program Linecount;
Your extra-credit question, for any of you college undergrads out there with
time on your hands, is to code the routine all three ways and figure out which
one really is faster... ;->
/mr/
>In a previous article, bmil...@solidoak.com (Brian Milburn) says:
>>Does anyone know a *fast* way to determine the number of lines in an
>>ASCII text file?
>>
>>Thanks,
>>
>>Brian
>>*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
>>| Brian Milburn bmil...@solidoak.com |
>>| Solid Oak Software CIS: 74774,551 - GO SOLIDOAK |
>>*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
>>
>>
>The Only way I know is the following:
>var Count: Longint;
> InFile: Text;
>begin
>Assign(Infile,'Somefile.txt');
>reset(Infile); {Not used in standard pascal, only in Turbo Pascal}
>Count:=0;
>While not eof(Infile) do begin
> readln(Infile);
> count:=count+1;
>end;
>close(Infile);
>end.
>Count would be the number of lines and the file would need to be closed
>at the end of the program.
>Hope it helps.
---------------------------------------------------
It would probably be faster to use BlockRead into the largest buffer you can
get and then scan for end-of-line characters. Using an incremented pointer
will probably save you a couple of instructions per cycle. (An n-character
end-of-line terminator requires the prefixing of the last n-1 characters to
the start of the buffer for each read except the first). For the ultimate in
speed use assembler, but only if you enjoy assembler programming, for the gain
in this case will not be so great.
var TB: TBufStream;
C: char;
i,LineCount: LongInt;
TB.Init('MyFile.Txt',stOpen,4096); {4096 is number of bytes to alloc for buffer
TB.Seek(0);
i := 0;
LineCount := 0;
While i < TB.GetSize ) do
begin
Repeat
TB.Read(C,1);
inc(i);
Until (i >= (TB.GetSize ) or (C = #10);
If (C = #10) and (i < TB.GetSize)
then begin
TB.Read(C,1);
inc(i);
if C = #13 then inc(LineCount);
end;
end;
Debug this and it'll do the job for you.
Note, you can eliminate the var 'i' from this by using TB.GetPos instead -
it may be faster to do this, but then again it may be faster to use i as I
did and avoid the repeated function calls. You could also just refer
directly to one of the TBufStream fields for the current file position
instead or using getpos.