replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Filelength: how to get it?
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Filelength: how to get it?

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Fortran77



Joined: 19 Dec 2007
Posts: 19

PostPosted: Mon Apr 20, 2009 5:18 pm    Post subject: Filelength: how to get it? Reply with quote

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 Embarassed
Back to top
View user's profile Send private message
JohnHorspool



Joined: 26 Sep 2005
Posts: 270
Location: Gloucestershire UK

PostPosted: Mon Apr 20, 2009 6:03 pm    Post subject: Reply with quote

just do a dummy read through the file:-

Code:

      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)
.
.
.
.
Back to top
View user's profile Send private message Visit poster's website
Fortran77



Joined: 19 Dec 2007
Posts: 19

PostPosted: Mon Apr 20, 2009 6:50 pm    Post subject: Reply with quote

Thanks, that's it.

Perhaps it's better for me to learn something more with this example; what means '(a)' and err=10?
Back to top
View user's profile Send private message
JohnHorspool



Joined: 26 Sep 2005
Posts: 270
Location: Gloucestershire UK

PostPosted: Mon Apr 20, 2009 9:24 pm    Post subject: Reply with quote

'(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.
Back to top
View user's profile Send private message Visit poster's website
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2402
Location: Yateley, Hants, UK

PostPosted: Mon Apr 20, 2009 10:55 pm    Post subject: Reply with quote

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.

Code:
      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
Back to top
View user's profile Send private message
IanLambley



Joined: 17 Dec 2006
Posts: 505
Location: Sunderland

PostPosted: Mon Apr 20, 2009 11:03 pm    Post subject: Reply with quote

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.

Code:

      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
Back to top
View user's profile Send private message Send e-mail
Fortran77



Joined: 19 Dec 2007
Posts: 19

PostPosted: Tue Apr 21, 2009 12:32 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
JohnHorspool



Joined: 26 Sep 2005
Posts: 270
Location: Gloucestershire UK

PostPosted: Tue Apr 21, 2009 8:06 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
Fortran77



Joined: 19 Dec 2007
Posts: 19

PostPosted: Tue Apr 21, 2009 4:12 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Fortran77



Joined: 19 Dec 2007
Posts: 19

PostPosted: Tue Apr 21, 2009 4:26 pm    Post subject: Reply with quote

This topic points out some solutions for beginners:

http://forums.silverfrost.com/viewtopic.php?t=133
Back to top
View user's profile Send private message
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2402
Location: Yateley, Hants, UK

PostPosted: Wed Apr 22, 2009 10:18 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group