- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I'm looking for information about the management of lists in fortran (using modules and object-oriented programming) as I'm going to start such a development but if someone has any experience in this field he will be welcomed. For more information I would like to use such lists to manage opened files in a program in order to close all of them at the same time when the program exits (without always knowing the number of open files nor their logical units). I thought that such lists would be useful for this purpose. Isn't it ?
I'm looking for information about the management of lists in fortran (using modules and object-oriented programming) as I'm going to start such a development but if someone has any experience in this field he will be welcomed. For more information I would like to use such lists to manage opened files in a program in order to close all of them at the same time when the program exits (without always knowing the number of open files nor their logical units). I thought that such lists would be useful for this purpose. Isn't it ?
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Mr. netphilou31
Fortran is not object-oriented (and many of us hope it never will be).
Most Fortran compilers will automatically close all open files when terminating, even due to an error, so this is not a huge issue. However, this is not guaranteed and good practice is to close all open files before exiting.
Units (including files) are only referenced by number. You cannot close a "file"in Fortran, only the unit number with which it was associated in the OPEN statement.
Maybe you might consider another language. What you want to do is straightforward to code in Fortran, just not in the manner that you you seem to want to do it.
Regards,
Keith
The closest thing to a "list" that I can think of is a derived type with a pointer. To my knowledge, there is no entity such as "list" defined in Fortran.
Fortran is not object-oriented (and many of us hope it never will be).
Most Fortran compilers will automatically close all open files when terminating, even due to an error, so this is not a huge issue. However, this is not guaranteed and good practice is to close all open files before exiting.
Units (including files) are only referenced by number. You cannot close a "file"in Fortran, only the unit number with which it was associated in the OPEN statement.
Maybe you might consider another language. What you want to do is straightforward to code in Fortran, just not in the manner that you you seem to want to do it.
Regards,
Keith
The closest thing to a "list" that I can think of is a derived type with a pointer. To my knowledge, there is no entity such as "list" defined in Fortran.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Fortran is not object-oriented (and many of us hope it never will be).
Well, I think that object-oriented way of thinking is A Good Thing. On the first place, that implies encapsulation. Other OO features, like inheritance, are not so crucial IMO. You can do OO (not to the full extent, but in many aspects) in any language, including Fortran. Decyk/Norton/Szymanski web site is the classical resource; I think it's worth reading even if you don't want to implement all of it.
To the original poster: your question is a bit too broad; there are many implementations of linked lists in Fortran(unfortunately, I don't have any link at hand). Perhaps you should start research from some Fortran portal, like Fortran Library. Also, comp.lang.fortran newsgroup is a better place than this Forum to discuss general language issues.
HTH
Jugoslav
Well, I think that object-oriented way of thinking is A Good Thing. On the first place, that implies encapsulation. Other OO features, like inheritance, are not so crucial IMO. You can do OO (not to the full extent, but in many aspects) in any language, including Fortran. Decyk/Norton/Szymanski web site is the classical resource; I think it's worth reading even if you don't want to implement all of it.
To the original poster: your question is a bit too broad; there are many implementations of linked lists in Fortran(unfortunately, I don't have any link at hand). Perhaps you should start research from some Fortran portal, like Fortran Library. Also, comp.lang.fortran newsgroup is a better place than this Forum to discuss general language issues.
HTH
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Jugoslav and netphilou31,
The linked list was what I was referring to in the paragraph that got misplaced at the end of my message. When I said that a list was not an "entity" I meant that no "built-in" or standard libraries operated on one. You can always write your own.
When I made my comment about "object-oriented" I was thinking of inheritance and operator overtyping, etc. Fortran has always provided data encapsulation, as I understand the term, in the sense that there are no global variables (at least before F90). The only things that go into, or out of, a subroutine are the arguments and values in common blocks with which it was compiled. Two subroutines can share a labled common block that is completely inaccesible to any other part of the program and the varible names used in the two don't even have to be the same. But we can continue our discussion in another thread, since this doesn't really help Mr netphilou31, who really needs to seriously think in some detail about what he wants to accomplish, and why, before anyone can give him any real help (sorry about referring to you in the third person, netphilou31).
Regards,
Keith
The linked list was what I was referring to in the paragraph that got misplaced at the end of my message. When I said that a list was not an "entity" I meant that no "built-in" or standard libraries operated on one. You can always write your own.
When I made my comment about "object-oriented" I was thinking of inheritance and operator overtyping, etc. Fortran has always provided data encapsulation, as I understand the term, in the sense that there are no global variables (at least before F90). The only things that go into, or out of, a subroutine are the arguments and values in common blocks with which it was compiled. Two subroutines can share a labled common block that is completely inaccesible to any other part of the program and the varible names used in the two don't even have to be the same. But we can continue our discussion in another thread, since this doesn't really help Mr netphilou31, who really needs to seriously think in some detail about what he wants to accomplish, and why, before anyone can give him any real help (sorry about referring to you in the third person, netphilou31).
Regards,
Keith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As usual Jugoslav has the right answers.
There is nothing about OO which prevents people to continue write old style (spaghetti cooked or raw) code.
Now to Net's question:
Here is sample code for linked lists. It used to be in the MSPS docs but it did not make it to CVF.
Linked list is one way. And BTW if anyone want to see the partial power of OO in an elegant language like fortran, you can write me and get my AutoArray "object" which is a kind of array which updates itself, and is optimized for speed and functionality. It replaces linked lists easily except that until I have f2k I can only implement it for a single type like Real(4). Linked lists can have arbitrary types.
!This is how you define a linked list
Type acct_typ
character*8 acct
integer(2) sumry !count summary could be an array etc
Type (acct_typ), pointer:: prev
end Type acct_typ
Type (acct_typ), pointer:: Acct_n,LastAcct_n !declare an instance
Nullify(LastAcct_n)
! end of definition (too bad nullify cannot go with the declarations)
! Say we want to update the number of transactions in an account
! and create a new account if none exists.
read(*,*)inputAcct,NewTransactions
! An example of how to go through the thing and add links
Acct_n => LastAcct_n
do while(associated(Acct_n))
if(Acct_n.acct .eq. inputAcct)then
Acct_n.Sumry=Acct_n.Sumry+NewTransactions
exit
endif
Acct_n=>Acct_n.prev
enddo
if(.not.associated(Acct_n))then
Acct_n => LastAcct_n
allocate(Acct_n)
Acct_n.acct=inputAcct
Acct_n.Sumry=NewTransactions
Acct_n.prev => LastAcct_n !point the new entry to the previous node(previous last)
LastAcct_n => Acct_n !update the end of list
endif
!This is how you deallocate the list
Acct_n => LastAcct_n
do while(associated(Acct_n))
Write(*,*)Acct_n.acct,Acct_n.Sumry !whatever... you could do the CLOSE here
LastAcct_n => Acct_n.prev
deallocate(Acct_n)
Acct_n => LastAcct_n
enddo
There is nothing about OO which prevents people to continue write old style (spaghetti cooked or raw) code.
Now to Net's question:
Here is sample code for linked lists. It used to be in the MSPS docs but it did not make it to CVF.
Linked list is one way. And BTW if anyone want to see the partial power of OO in an elegant language like fortran, you can write me and get my AutoArray "object" which is a kind of array which updates itself, and is optimized for speed and functionality. It replaces linked lists easily except that until I have f2k I can only implement it for a single type like Real(4). Linked lists can have arbitrary types.
!This is how you define a linked list
Type acct_typ
character*8 acct
integer(2) sumry !count summary could be an array etc
Type (acct_typ), pointer:: prev
end Type acct_typ
Type (acct_typ), pointer:: Acct_n,LastAcct_n !declare an instance
Nullify(LastAcct_n)
! end of definition (too bad nullify cannot go with the declarations)
! Say we want to update the number of transactions in an account
! and create a new account if none exists.
read(*,*)inputAcct,NewTransactions
! An example of how to go through the thing and add links
Acct_n => LastAcct_n
do while(associated(Acct_n))
if(Acct_n.acct .eq. inputAcct)then
Acct_n.Sumry=Acct_n.Sumry+NewTransactions
exit
endif
Acct_n=>Acct_n.prev
enddo
if(.not.associated(Acct_n))then
Acct_n => LastAcct_n
allocate(Acct_n)
Acct_n.acct=inputAcct
Acct_n.Sumry=NewTransactions
Acct_n.prev => LastAcct_n !point the new entry to the previous node(previous last)
LastAcct_n => Acct_n !update the end of list
endif
!This is how you deallocate the list
Acct_n => LastAcct_n
do while(associated(Acct_n))
Write(*,*)Acct_n.acct,Acct_n.Sumry !whatever... you could do the CLOSE here
LastAcct_n => Acct_n.prev
deallocate(Acct_n)
Acct_n => LastAcct_n
enddo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Mr. Rahzan, Jugoslav and Netphilou31,
I apologize for my replies.
Regards,
Keith
I apologize for my replies.
Regards,
Keith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
For all who reply to this thread. Great thanks. I recognize I am a little bit late but I completely forget my original post and I have not been on this forum for a while. Special thanks to rahzan for the piece of code. This is exactly what I would do (each item of the list handling the name and logical unit associated with the open file). I hope you will see my message even if it a little bit late.
Thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I recall a comp.lang.fortran newsgroup discussion on this topic. Google should help in finding it. Fortran has always (at least since f77) closed all open files upon exit. If your program wanted some failure reporting, for example, your technique might be wanted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, linked lists are elegant, but for a reasonably small number of files you don't need a linked list. You can just keep of table of unit numbers or use the inquire statement. When needing to make sure all are closed, just close all the unit numbers regardless of open status.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page