Count lines in a file quickly - 2 -

[Autolink] Menu

 
/* another sample code to count the lines in a file quickly           */
/*                                                                    */
/* Source: Oliver Heidelbach (see EMail Addresses)                    */
/*                                                                    */
/* see also Count lines in a file quickly                             */
/*                                                                    */

                        /* name of the file                           */
  testFile = "C:\OS2\INISYS.RC"

  call time "r"

  noOfLines = CountLines( testFile )

  if noOfLines >= 0 then
  do
    say "The file " || testFile || " contains " || ,
        noOfLines || " lines."
    say "It took " || time( "e" ) || " seconds to get the number."

  end /* if */
  else
    say "Error reading the file " || testFile

exit 0

/* ------------------------------------------------------------------ */
/* function: Get the number of lines in a file                        */
/*                                                                    */
/* call:     CountLines( fileName )                                   */
/*                                                                    */
/* where:    fileName - name of the file                              */
/*                                                                    */
/* returns:  n                                                        */
/*             if n is >= 0 then it is the number of lines            */
/*             if n is < 0 an error occured                           */
/*                                                                    */
CountLines: PROCEDURE
  parse arg fileName

  if stream( fileName, "c", "QUERY EXISTS" ) <> "" then
  do
    if stream( fileName, "c", "OPEN READ" )  = "READY:" then
    do

      thisRC = 0

      buf = charin( File,1,stream( filename,'c','query size' ) )
      call stream filename, 'c', 'close'

      buf = translate( buf,copies( ' ',10 ) || '1' || copies( ' ',245 ) )
      buf = space( buf,0 )
      thisRC = length( buf )

    end /* if stream( fileName, "c", "open read" )  = "READY:" then */
    else
      thisRC = -1

  end /* if stream( fileName, "c", "QUERY EXISTS" ) <> "" then */
  else
    thisRC = -2

RETURN thisRC


[Back: Count lines in a file quickly]
[Next: Read a file into a compound variable]