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

OPEN (file='...') won't recognize file name

John_Helsdon
Beginner
584 Views
This may be too basic here, but when I try to open a file name through a directory path, FORTRAN version w_cprof_p_11.1.038, I get error#6404 (and others) and it tells me that it must be a character string, even though I define it with quotes. I, for the life of me, can't figure this out. It works fine in non-Intel compilers. Code is attached. I tried to define part of the path as "fname", but it didn't like that, so I tried to just input file= with the file name in quotes. No go. BTW, the print of fname concatenated with the rest of the directory path worked just fine when I commented out the OPEN statement. Can anyone help?

[plain]Program test_file
implicit none
character(len=68), parameter :: fname = 'c:Documents and SettingsJohn Helsdon'&
'My DocumentsFortrancharge'
integer :: i, j, k
real :: dx, dy, dz
print *, fname//'input.dat'
open(unit=100, file='c:Documents and SettingsJohn HelsdonMy DocumentsFortran'&
'chargeinput.dat', status=unknown)
read(100, *) i,j,k,dx,dy,dz
print *, 'values are ', i,j,k,dx,dy,dz
close(100)
end program test_file[/plain]

0 Kudos
3 Replies
DavidWhite
Valued Contributor II
584 Views
This may be too basic here, but when I try to open a file name through a directory path, FORTRAN version w_cprof_p_11.1.038, I get error#6404 (and others) and it tells me that it must be a character string, even though I define it with quotes. I, for the life of me, can't figure this out. It works fine in non-Intel compilers. Code is attached. I tried to define part of the path as "fname", but it didn't like that, so I tried to just input file= with the file name in quotes. No go. BTW, the print of fname concatenated with the rest of the directory path worked just fine when I commented out the OPEN statement. Can anyone help?

[plain]Program test_file
implicit none
character(len=68), parameter :: fname = 'c:Documents and SettingsJohn Helsdon'&
'My DocumentsFortrancharge'
integer :: i, j, k
real :: dx, dy, dz
print *, fname//'input.dat'
open(unit=100, file='c:Documents and SettingsJohn HelsdonMy DocumentsFortran'&
'chargeinput.dat', status=unknown)
read(100, *) i,j,k,dx,dy,dz
print *, 'values are ', i,j,k,dx,dy,dz
close(100)
end program test_file[/plain]


Your folder name for the file has spaces in it. I suggest that you add an extra level of quotes, eg,

file = "'c: ... " &
" ... '", ... etc

I have changed your single quotes to double quotes and then started and finished the string with a set of single quotes.
This means that the whole string, including the single quotes will be used to try and access the file.

Regards,

David
0 Kudos
IanH
Honored Contributor III
584 Views
Apologies if these are just code transcription issues, but...

On line 9, the 'unknown' argument to the status specifier needs to have quotes around it to make it into a character constant, otherwise the compiler thinks it is a variable (hence the error #6404 which is actually about a variable not having a type).

open(..., status='unknown')

Also your line continuation may not be doing what you think it is doing. For both your fname parameter definition and the file argument for the open statement you've got something like:

fname='first_part'&
'second_part'

According to free form line continuation rules, that actually looks like the following to the compiler once the lines have been spliced together:

fname='first_part''second_part'

Note the two sequential quotes in the middle of the "source code representation" of the character constant. That represents a single quote in the actual value for the character constant. This is probably not what you actually wanted in the middle of your filename (and hence you would see runtime errors if you used fname to form part of a file).

If you have to split a constant across two lines consider either using an explicit concatenation operator (as for your print statement, but just have things on two lines):

fname='first_part' &
// 'second_part'

or explicitly use the free source method of continuing character constants:

fname='first_part&
&second_part'

(The leading ampersand on the second line is optional, but it makes things clear about what is supposed to be in the constant and what is not).

IanH
0 Kudos
John_Helsdon
Beginner
584 Views
The replies gave me the information I needed. Thanks for taking the time to help, guys.
0 Kudos
Reply