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 

Problem with the Open Statement
Goto page 1, 2  Next
 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
KL
Guest





PostPosted: Sun Sep 02, 2007 5:22 pm    Post subject: Problem with the Open Statement Reply with quote

Klaus Lassmann Tel. 0045 74441221
Skeldevigvej 12
DK - 6310 Broager
Danmark


Dear Paul,

I have a little problem with the OPEN statement. Just assume that the directory

'd:\ftn95\tests\test41'

exists. In the attached program I have misspelled this directory (a slash instead of a back-slash). The following OPEN statement should detect this, but this is not the case. On my machine the program prints-out the wrong directory instead of writing 'Directory\File does not exist'. Please see the attached files Run.bat and test41.f95.

I have run the test with FTN95 5.10.

Best regards,

Klaus Lassmann

File run.bat:
del comp.lis
del *.obj
del *.exe

ftn95 Test41.f95 /Checkmate /link >> comp.lis
sdbg Test41.exe

File test41.f95
Winapp

Program Test41
Implicit None
Character (len = 128 ) :: dir
Integer :: nUnit

! dir = 'd:\ftn95\tests\test41'
dir = 'd:/ftn95\tests\test41'
nUnit = 10
Open ( Unit = nUnit, status = 'unknown', err = 100, File = trim (dir)//'\Test' )

Write (*,*) trim (dir)//'\Test'
stop

100 continue
Write (*,*) 'Directory\File does not exist'

End Program Test41
Back to top
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7927
Location: Salford, UK

PostPosted: Sun Sep 02, 2007 9:10 pm    Post subject: Reply with quote

Klaus

As far as I know the Fortran standard does not say what should happen when the folder does not exist.

If the file already exists, you should use status = 'old' in the OPEN statement (as you will know).

The natural choice is for FTN95 to try to create the folder as well as the file (when the folder does not exist). This would happen when you try to write to or close the file and I guess you should get an error report of some kind if the folder cannot be created at this point.

There will be an FTN95 subroutine that allows you to test if a given folder already exists but, off hand, I do not remember its name.
Back to top
View user's profile Send private message AIM Address
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Mon Sep 03, 2007 10:55 am    Post subject: Re: Reply with quote

PaulLaidler wrote:

There will be an FTN95 subroutine that allows you to test if a given folder already exists but, off hand, I do not remember its name.


It's logical function fexists@ ...
Back to top
View user's profile Send private message Send e-mail
KL
Guest





PostPosted: Mon Sep 03, 2007 5:59 pm    Post subject: Problem with the Open statement Reply with quote

Dear Paul,

thank you for your quick reply. My problem is the following: I am writing a program, where the program asks the user for a directory name and the user has to input the full directory. Of course the program checks whether this directory exists. I have not found a specific function for this (fexists seems to be not appropriate) and therefore I open a new file with an arbitrary name in the directory given by the user. This is the reason for the strange statement

Open (.. File = trim (user_directory)//'\arbitrary_file-name', err = ...

If this file cannot be opened, the directory does not exist and the program should jump to err = ... This works perfectly for all errors in the directory name, except, if a back-slash is misspelled and written as a slash. As long as the arbirary name of the file fulfills all Fortran requirements, I do not see that I violate any Fortran rule. Of course, it would be more elegant, if I could use a specific function which tells me if a directory exists. But should not the Open statement lead to an error, if the file cannot be created?

Best regards, Klaus
Back to top
KL
Guest





PostPosted: Tue Sep 04, 2007 7:17 am    Post subject: Problems with OPEN Reply with quote

PS. May be that I have not expressed myself precisely: By "Fortran rules" I mean the standard plus the FTN95 extensions. And I understood that these extensions include a full file name consisting of directory plus the file name.
Back to top
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Tue Sep 04, 2007 10:00 am    Post subject: Reply with quote

Klaus,

I have previously done exactly what you are doing, to find out whether a directory exists i.e. tag on a made-up file name to the directory in question, and try to open it. But I have been using fexists@ for a while now, to query the existence of both files and folders. The documentation suggests it is for files only - is that what you mean when you say it does not seem to be appropriate? I think I tried feeding it a path, to see what would happen, because it would have been very useful if it worked - and found that it did. Perhaps I've just been lucky, and I shouldn't rely on it.

Paul, can you advise?

Andy
Back to top
View user's profile Send private message Send e-mail
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7927
Location: Salford, UK

PostPosted: Tue Sep 04, 2007 3:12 pm    Post subject: Reply with quote

You may need to append a wild card "*.*" for the file name.
Back to top
View user's profile Send private message AIM Address
KL
Guest





PostPosted: Tue Sep 04, 2007 5:06 pm    Post subject: Problems with Open Reply with quote

Dear Paul, Dear Andy,

many thanks for your help. Unfortunately, I have still problems. Please have a look to the following test program. You can be absolutely sure that the directory, which I check does exist and there are several files in it.

Winapp

Program Test41
Implicit None
Character (len = 128 ) :: dir
Integer (kind = 3) :: error_code
Logical (kind = 3) :: fexists@
Integer :: nwr

nwr = 10
Open (Unit = nwr, file = 'output' )

! --- The directory d:\ftn95\tests\test41 exists
! and holds several files !!!!!!!!!!!!

dir = 'd:\ftn95\tests\test41\*.*'
! dir = 'd:/ftn95\tests\test41\*.*'

If ( fexists@ ( dir, error_code ) ) Then
write (nwr,*) 'yes'
write (nwr,*) 'error_code = ', error_code
write (nwr,*) dir
Else
write (nwr,*) 'no'
write (nwr,*) 'error_code = ', error_code
write (nwr,*) dir
End If

dir = 'd:\ftn95\tests\test41'
! dir = 'd:/ftn95\tests\test41'

If ( fexists@ ( dir, error_code ) ) Then
write (nwr,*) 'yes'
write (nwr,*) 'error_code = ', error_code
write (nwr,*) dir
Else
write (nwr,*) 'no'
write (nwr,*) 'error_code = ', error_code
write (nwr,*) dir
End If

End Program Test41


I cannot understand the result:

no
error_code = 123
d:\ftn95\tests\test41\*.*

yes
error_code = 0
d:\ftn95\tests\test41

Following you, Paul, the directory does not exist, following you, Andy, the result is correct. However, one should never rely on a "trick" that is actually not explicitely allowed.

It is interesting to note, that the result is identical, when instead of the correct statement, the misspelled directory (slash instead of back-slash, see commented line) is used.

I am sure that I am making somewhere a mistake, but I do not see where.

Best regards, Klaus
Back to top
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Tue Sep 04, 2007 5:20 pm    Post subject: Re: Problems with Open Reply with quote

KL wrote:
Following you, Paul, the directory does not exist, following you, Andy, the result is correct. However, one should never rely on a "trick" that is actually not explicitely allowed.


Agreed. That was why I asked Paul's advice.

KL wrote:
It is interesting to note, that the result is identical, when instead of the correct statement, the misspelled directory (slash instead of back-slash, see commented line) is used.


Are you saying what you appear to be saying here - that fexists@, used "my way", still correctly diagnoses the existence of your directory, even when it is spelt including a slash rather than a backslash? Rolling Eyes This is a good thing, isn't it?

Andy
Back to top
View user's profile Send private message Send e-mail
KL
Guest





PostPosted: Tue Sep 04, 2007 6:13 pm    Post subject: Problems with OPEN Reply with quote

Dear Andy,
Dear Andy

It seems to me that both methods that I have used to find out whether a directory exist, namely
a) by the OPEN statement or
b) by fexists@
do not differentiate between a slash and a back-slash in the directory name. I do not see that this is an advantage since this produces an error when you pass such a misspelled directory name to another procedure.

I guess that for the OPEN statement and for fexists@ a similar piece of code is applied, since you cannot open a file in a non-existing directory. This task looks similar to fexists@. Lets wait for Pauls' answer.

Best regards, Klaus
Back to top
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7927
Location: Salford, UK

PostPosted: Wed Sep 05, 2007 8:47 am    Post subject: Reply with quote

I am aware that in some circumstances "/" and "\" and be used interchangably in a path but I do not have the details to hand.

As a quick and easy solution you could include something like the following...

Code:

character(256) path
integer pos
path="c:\ftn95/test"
do
 pos = scan(path, "/")
 if(pos == 0) exit
 path(pos:pos) = "\"   
enddo
print*,path
end
Back to top
View user's profile Send private message AIM Address
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Wed Sep 05, 2007 2:59 pm    Post subject: Reply with quote

I just tried running your original code snippet, to find out what you meant by "On my machine the program prints-out the wrong directory instead of writing 'Directory\File does not exist'".

You say you ran the test with v5.10. I just upgraded to v5.10 this morning - and on mymachine (Intel processor, Windows XP), the program prints out 'Directory\File does not exist' - which is what it ought to do!

What machine/OS are you using?

Andy
Back to top
View user's profile Send private message Send e-mail
KL
Guest





PostPosted: Wed Sep 05, 2007 4:09 pm    Post subject: Problems with OPEN Reply with quote

Andy,

the results, which I obtain are given in one of my last e-mails. The result is the same whether or not I misspell the directory.

Klaus
Back to top
KL
Guest





PostPosted: Wed Sep 05, 2007 4:24 pm    Post subject: Reply with quote

Paul, thank you for your proposal. I had already written a similar code fragment to solve the problem, but this does not really satisfy me.

Have you had the time to look at my code concerning fexists@? Why does it give the error code 123?

Best regards,

Klaus
Back to top
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Wed Sep 05, 2007 4:33 pm    Post subject: Reply with quote

But those results are obtained with a different version of the test to that which you posted originally. I am saying that I ran the test as originally posted - using your trick of opening an arbitrary-named file in the directory to be tested. I got a different result to you - the desired result -the OPEN statement failed (as it should).

I added the IOSTAT specifier into the OPEN statement. It returns error code 10021, which is way out of range of any of the values listed as IOSTAT return values in the help file - so there's still a mystery associated with the meaning of this error code - but at least on my machine, your program behaves as it ought, even if it's a little enigmatic as to exactly why.

We both claim to be using v5.10 of FTN95. That's why I asked you what machine and OS you are using.

Andy
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
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