Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

file inquire

yjyincj
Beginner
649 Views
I compiled this following code in Fedora 11 with intel fortran compiler 11. If the 'filename' is in the same folder of a.out, it find the file. Or it will not find the file even I gave the full path of the file, like ''/home/yj/e.txt''
Why? I never have such problem in Windows os.
program main
implicit none
character(len=300)::filename
logical::exists
write(*,*) 'Input your file name'
read(*,*) filename
inquire(file=filename,exist=exists)
if (exists) then
write(*,*) 'file exists'
else
write(*,*)'I did not find that file.'
end if

stop
end program
0 Kudos
3 Replies
mrentropy1
Beginner
649 Views


You have problems when you enter the complete path in linux, but not in Windows, since Windows uses and linux uses /, and everything after a / is stripped.

You can fix this by changing your read statement to

read (*,100) filename
100 format ((A))

However, the program as posted shouldn't have any problems just entering the filename without the full path, so I'm confused by that....

By the way,might not be a bad idea (I think) to change your inquire call to:

inquire(file=TRIM(filename),exist=exists)

Peter
0 Kudos
yjyincj
Beginner
649 Views
Quoting - mrentropy


You have problems when you enter the complete path in linux, but not in Windows, since Windows uses and linux uses /, and everything after a / is stripped.

You can fix this by changing your read statement to

read (*,100) filename
100 format ((A))

However, the program as posted shouldn't have any problems just entering the filename without the full path, so I'm confused by that....

By the way,might not be a bad idea (I think) to change your inquire call to:

inquire(file=TRIM(filename),exist=exists)

Peter
Thank you Peter. If the file is in the same folder (i.e. without path), that program runs ok.
0 Kudos
Hirchert__Kurt_W
New Contributor II
649 Views
Quoting - yjyincj
Quoting - mrentropy


You have problems when you enter the complete path in linux, but not in Windows, since Windows uses and linux uses /, and everything after a / is stripped.

You can fix this by changing your read statement to

read (*,100) filename
100 format ((A))

However, the program as posted shouldn't have any problems just entering the filename without the full path, so I'm confused by that....

By the way,might not be a bad idea (I think) to change your inquire call to:

inquire(file=TRIM(filename),exist=exists)

Peter
Thank you Peter. If the file is in the same folder (i.e. without path), that program runs ok.

A few minor comments:

1. You shouldn't need the extra parentheses in the format statement. I.e., you should be able to use

100 format(A)

2. Some people prefer to include small formats directly in a read or write statement. E.g.,

read(*,'(A)') filename

3. You could even continue using read(*,*) filename if you make a small change in how you enter the input. Instead of entering

/path/to/filename

you could enter either

'/path/to/filename'

or

"/path/to/filename"

Either form of quoting causes the "/"s in the path to be read as data instead of as a signal to end the input.

-Kurt
0 Kudos
Reply