Silverfrost Forums

Welcome to our forums

Filelength: how to get it?

20 Apr 2009 4:18 #4436

Hello, this seems a basic question, but the more I search... the less I find.

I want to read a text file that looks like this:

..... 123 4567 10000 200000 213 5678 20000 300000 .....

As you see it looks like a database.

To make some calculations, I need to know how many records there are in the file.

Is there a way to know that?

Thanks 😮ops:

20 Apr 2009 5:03 #4438

just do a dummy read through the file:-

      character*80 text

      open(unit=15,file='file.dat',status='old')

      line=1
    5 read(15,'(a)',err=10)text

      line=line+1
      go to 5

   10 write(6,*)'There are ',line,' lines in the file'

      rewind(unit=15)
.
.
.
.
20 Apr 2009 5:50 #4439

Thanks, that's it.

Perhaps it's better for me to learn something more with this example; what means '(a)' and err=10?

20 Apr 2009 8:24 #4440

'(a)' is just the format for reading in text strings err=10 means that the program jumps to statement 10 when there is an error in the read, or in other words when it tries to read beyond the end of the file the program jumps out of the read loop.

These should be in any elementary fortran tutorial book.

20 Apr 2009 9:55 #4441

John's code is simple, and illustrates the method perfectly. However, as well as ERR= there is also an END= option. That will jump to the given statement when an end of file is encountered (the case desired). Keeping an ERR= as well will also permit you to read each line with the correct format and will demonstrate that the file is 'readable' to its end.

      Integer k(4)

      open(unit=15,file='file.dat',status='old') 

      line=1 
    5 read(15,'*,end=10, err=20) k

      line=line+1 
      go to 5 

   10 write(6,*)'There are ',line,' lines in the file' 

      rewind(unit=15)

C      go and do something with it .....

  20   write(6,*) 'Only the first ',lines,' lines of file are readable'

As an alternative, if each line has a fixed length, you can call the FTN95 library routine FILE_SIZE@ (rather simpler than FILEINFO@) which gives the file length in bytes and then divide by the line length (taking into account CR LF etc at the end of each line).

Eddie

20 Apr 2009 10:03 #4442

You don't actually have to read anything from the line to get the file size, here is a subroutine to find the length of a previously opened sequential file.

      subroutine get_sequential_length(ichan,ilen)
      ilen = 0
      do while (ilen .gt. -1)
        read(ichan,*,err=5000)
        ilen = ilen + 1
      enddo
 5000 continue
      end

Ian

20 Apr 2009 11:32 #4443

John, Eddie and Ian I understood well your posts. Although I'm going on digesting them, I learned very much

For a beginner like me, some old fortran knowledge is difficult to met now in books or tutorials.

I thank you your patience.

21 Apr 2009 7:06 #4445

ummm .... what do current fortran tutorials contain?

The only tutorial I ever read (and still have) is 'Computing with FORTRAN IV A practical Course' by Donald M. Monro published in 1977 - good in its day but not something I would recommend now, perhaps there is a Fortran95 version available.

21 Apr 2009 3:12 #4449

Thanks John

that's a good issue: where exists a good text, dealing with basics concepts that are common with Fortran77 and Fortran90/95?

After looking for a while and having met a lot of good links, I'm now reading this one: Professional Programmer's Guide to Fortran 77 (Clive G. Page, University of Leicester, UK, 2005) that is a reprint (with some changes?) of a book published by Pitman in 1988.

In this text, I understood my difficulties about ' '(a)' and err=10 '; although I knew that after the WRITE or READ statement I can specify the format inside curle brackets, I didn't know that it was possible to put more than 2 'arguments', because nowadays, usually, examples are like this:

WRITE (,) a b c d WRITE (15,*) a b c d WRITE (15,'(a,1x,a,1x,a,1x,a))

A very old example in that book explains what means (,):

write (UNIT=, FMT=) 'HELLO'

It's easy for a beginner to only admit that inside curle brackects we must define two things only.

BTW, I've not seen in this forum some topic related with Fortran texts or tutorials; I'll search.

It would be nice to have a corner for present beginners to see good informational links

Frank

21 Apr 2009 3:26 #4450

This topic points out some solutions for beginners:

https://forums.silverfrost.com/Forum/Topic/67

22 Apr 2009 9:18 #4454

I suspect if you ask a number of users of FTN95, you will get two divergent views. You will find those, like me, who basically continue to program in Fortran-77, and those who have wholeheartedly adopted Fortran-90 and -95 (the latter being a 'tidy up', as the major changes occurred in -90).

One or two Fortran-90 things have crept in: comments on the same line as statements after !, and initialisation of a whole array in one statement are two that have got to me.

Since the whole of Fortran-77 continues to be 'allowed' in -90 and -95, then it seems to be entirely sensible that I continue to tweak programs written in the last 40 years, but not to re-write any of them. Programming isn't my 'main job' anyway. I would take the opposite view if I were a young man and wanted my stuff to continue to work 40 years hence!

There used to be lots of books on Fortran. Now there aren't. The older books like Monro, McCracken, Etter etc were all for Fortran-66 or -77. The only chap who seems to write a lot for Fortran-9x is Metcalf (including Metcalf & Reid). If you look on the web, there are several resources for knowledgeable Fortran-77 programmers transferring to the later standards. They all take the view that the new features are 'better' than the old, something that is supported by sometimes dodgy arguments (like 'I say it is, therefore it must be so' (usually the author for that is on the Fortran Committee), or 'use lower case, so that you can distinguish between o and 0', conveniently forgetting that lower case makes it difficult to distinguish between l and 1 - this is simply a matter of which font you use).

After using Fortran since 1969 (and it was Fortran-66 or Fortran-IV then), I continue to find things in it I didn't know. More to the point, there are new things to learn in Clearwin all the time. Raw Fortran has no graphics, and programs written in it have the same appearance as things one wrote 40 years ago. FTN95's libraries and Clearwin (or .NET support) set it in a different league.

People on this forum are usually very helpful (e.g. Ian and John's suggestions). Ian uses Fortran-90 style ...

Eddie

Please login to reply.