comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: how to find number of lines in an ASCII file?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: how to find number of lines in an ASCII file? [message #12558 is a reply to message #12549] Wed, 19 August 1998 00:00 Go to previous messageGo to previous message
Kevin Ivory is currently offline  Kevin Ivory
Messages: 71
Registered: January 1997
Member
Jason Li wrote:
>> I want to read them all and save into an array: data[8, numberOfLines]. But
>> I don't know numberOfLines in the file before hand. What is the most efficient
>> way to find that out?

Phillip & Suzanne David wrote:
> ...
> while (not eof(lun)) do begin
> readf, lun, line
> text = [text, line] ; <---
> endwhile
> ...

The marked line is really going to slash your memory, since new memory
will have to be allocated in each loop.

As for the efficient ways:
1. Simply count the lines in the loop above
(best to use a long integer in case you have more the 32000 lines).
2. Even more efficient is to spawn the 'wc -l' command if you are on
a unix system. Alas, this is not platform independent.

Attached is my n_lines.pro which can do both.

Best regards
Kevin
--
Kevin Ivory Tel: +49 5556 979 434
Max-Planck-Institut fuer Aeronomie Fax: +49 5556 979 240
Max-Planck-Str. 2 mailto:Kevin.Ivory@linmpi.mpg.de
D-37191 Katlenburg-Lindau, GERMANY http://www.gwdg.de/~kivory2/
------------------------------------------------------------ ------------
; Time-stamp: <n_lines.pro Fri Feb 13 14:18:47 MET 1998>

function n_lines, file, unix=unix
;+
; Purpose:
; Count the number of lines in a file.
; Argument:
; file string name of file
; Optional keywords:
; unix int spawn the unix 'wc' system command
; Restrictions:
; Spawning the 'wc' unix system command is significantly faster than the
; portable IDL method - but it is not platform independent. :-(
;-
if n_params() lt 1 then begin
message, /info, 'Filename required.'
return, -1
endif

; if !version.os_family eq 'unix' then begin ; no keyword setting required
if keyword_set(unix) then begin
spawn, ['wc', '-l', file], result, /noshell
nlines = long(result(0))
endif else begin
openr, lun, file, /get_lun, error=err
if err ne 0 then begin
message, /info, 'Error reading file ' + file
return, -1
endif

nlines = 0l & line = ''
while not eof(lun) do begin
readf, lun, line
nlines = nlines + 1l
endwhile
free_lun, lun
endelse
return, nlines
end
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: general ASCII table import (was: how to find number of lines in an ASCII file?)
Next Topic: basic image processing library?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 17:17:24 PDT 2025

Total time taken to generate the page: 0.00398 seconds