Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding total number of lines in a file

5 views
Skip to first unread message

Arindam Chakraborty

unread,
Jul 27, 2003, 6:35:24 PM7/27/03
to
Hi,
Is there a subroutine/function in fortran(77 or 90) that gives the
total number of lines in a file. I am currently using "system" call
with "wc -l"
option.
Thanks in advance
-Ari

Paul Curtis

unread,
Jul 27, 2003, 8:39:20 PM7/27/03
to
nlines = 0
OPEN (lunit, file = 'whatever')
DO
READ (lunit,*, END=10)
nlines = nlines + 1
END DO
10 CLOSE (lunit)

Richard Maine

unread,
Jul 29, 2003, 1:29:42 PM7/29/03
to
Paul Curtis <pcu...@kiltel.com> writes:

Note that Paul's code is essentially what "wc -l" does anyway. Most
current operating systems don't store information about the number of
records in a file, so the only way to find it out is to read through
the file and count them. Even if the Fortran language defined such a
query, that's how it would be implemented behind your back on most
current actual operating systems.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain

Glen Herrmannsfeldt

unread,
Jul 29, 2003, 6:40:45 PM7/29/03
to

"Richard Maine" <nos...@see.signature> wrote in message
news:uey8yhj...@altair.dfrc.nasa.gov...

> Paul Curtis <pcu...@kiltel.com> writes:
>
> > nlines = 0
> > OPEN (lunit, file = 'whatever')
> > DO
> > READ (lunit,*, END=10)
> > nlines = nlines + 1
> > END DO
> > 10 CLOSE (lunit)
> >
> > Arindam Chakraborty wrote:
> >
> > > Is there a subroutine/function in fortran(77 or 90) that gives the
> > >total number of lines in a file. I am currently using "system" call
> > >with "wc -l" option.
>
> Note that Paul's code is essentially what "wc -l" does anyway. Most
> current operating systems don't store information about the number of
> records in a file, so the only way to find it out is to read through
> the file and count them. Even if the Fortran language defined such a
> query, that's how it would be implemented behind your back on most
> current actual operating systems.

Well, the unix implementations of wc read the file in some sized blocks and
count the number of '\n' characters. It can do this without, for example,
needing a buffer big enough to store a line into.

I have had files with millions of characters and words, but no lines.

-- glen


Richard Maine

unread,
Jul 29, 2003, 6:59:32 PM7/29/03
to
"Glen Herrmannsfeldt" <g...@ugcs.caltech.edu> writes:

> "Richard Maine" <nos...@see.signature> wrote in message
> news:uey8yhj...@altair.dfrc.nasa.gov...

> > Note that Paul's code is essentially what "wc -l" does anyway...

> Well, the unix implementations of wc read the file in some sized blocks and
> count the number of '\n' characters. It can do this without, for example,
> needing a buffer big enough to store a line into.

That's close enough to be what I intended to cover with my "essentially".
The fundamental approach theere is still "read through the file and
count"; it just tries to be efficient about doing that.

BTW, Paul's code doesn't necessarily need a buffer big enough to hold a
record either as he doesn't read any data items. Depends on the
particular compiler runtimes whether or not they would hav eto be able
to buffer the whole record or not.

Glen Herrmannsfeldt

unread,
Jul 29, 2003, 10:15:51 PM7/29/03
to

"Richard Maine" <nos...@see.signature> wrote in message
news:uebrvdj...@altair.dfrc.nasa.gov...

> "Glen Herrmannsfeldt" <g...@ugcs.caltech.edu> writes:
>
> > "Richard Maine" <nos...@see.signature> wrote in message
> > news:uey8yhj...@altair.dfrc.nasa.gov...
>
> > > Note that Paul's code is essentially what "wc -l" does anyway...
>
> > Well, the unix implementations of wc read the file in some sized blocks
and
> > count the number of '\n' characters. It can do this without, for
example,
> > needing a buffer big enough to store a line into.
>
> That's close enough to be what I intended to cover with my "essentially".
> The fundamental approach theere is still "read through the file and
> count"; it just tries to be efficient about doing that.
>
> BTW, Paul's code doesn't necessarily need a buffer big enough to hold a
> record either as he doesn't read any data items. Depends on the
> particular compiler runtimes whether or not they would hav eto be able
> to buffer the whole record or not.

The tradition I used to know for Fortran was that each read started with a
new record (line), and anything left at the end of the read statement was
ignored. In that case, yes, it should be able to ignore it. It isn't
easy to write the loop to do that, and it isn't easy to test all the cases,
either.

-- glen


0 new messages