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

problems with input files

kageist
Beginner
745 Views
I have problems all the time with reading input files into a program. I'm not sure if the problem lies with the open statement, read statement, or input file. With several of them, after I play with it for a while, it ends up working, but I'm never sure why and often the program looks the same to me after it works as it did when the problem occured.

Here is an example of what most of my statements look like:


open(unit=10, file='res3', status='old')
read(10,*) R1, R2, R3

res3 is a text file that looks like this:
1000.0
1100.0
2000.0

Any suggestions would be appreciated.

0 Kudos
11 Replies
kageist
Beginner
745 Views
If it helps, I have much more success writing to an ouput file.
0 Kudos
Michael_J_Slater__In
New Contributor I
745 Views
This code executes fine, assuming R1,R2,R3 are of type real.
Are you having any specific problems or errors?
0 Kudos
kageist
Beginner
745 Views
Here's the rest of the program:


program resis1

real R1, R2, R3, RC

open(unit=10, file='res3')

read(10,*) R1, R2, R3

print *, R1, R2, R3

RC = 1.0/(1.0/R1 + 1.0/R2 + 1.0/R3)

print *, RC

pause

end


The file with the numbers is a .txt file
When I try to run it, it says it breaks at the read line.

0 Kudos
Steven_L_Intel1
Employee
745 Views
Please attach the input file as a .txt file (see below for instructions on adding files.) You wrote what the file "looks like" but that may not accurately represent what it is.
0 Kudos
mecej4
Honored Contributor III
745 Views
So far, you have not described what kinds problems you ran into with the kind of clarity that is needed to diagnose the problems. Vague statements such as "it breaks at the read line" do not help.

It would help if you attached files as they are instead of pasting text in-line in your posts, because small differences in text file format can sometimes cause errors on your side that cannot be reproduced at this end.

Do note that when you use list-directed input, sufficient lines will be consumed to fill the input list. In your example, since the input file contains only one real number per line, the read statement consumes three lines. Different rules apply when you use formatted input.
0 Kudos
kageist
Beginner
745 Views

here's the text file

0 Kudos
kageist
Beginner
745 Views
I see your point. Part of the problem is I'm still so new at this I'm not even sure if I'm going through the process right.
After the program and text file are written (the text file is now attached to my other reply) I click debug and a message comes up that says "resis1.exe has triggered a break point". I click break and the message leaves and there is a green arrow pointing to the read line. So far that's all I know. It doesn't say anything in the error list.
Like I said, since I'm so new to this, I'm guess I'm not sure what else I should do to find errors and make sure the program is running right.
0 Kudos
JohnNichols
Valued Contributor III
745 Views

One of the problems I see is that the text file has the numbers on three lines and you are using a read statement that in a normal setting would expect the numbers on one line only.

Try putting in three read statements or a loop or put all the numbers on one line of the text file.

I would also not use print, but write with a format statement so you can control the output.

There are several excellent and cheap Fortran books that teach this sort of stuff.

Good luck

JMN

0 Kudos
kageist
Beginner
745 Views
That makes sense. Thank you!
0 Kudos
Les_Neilson
Valued Contributor II
745 Views

Can you tell us :
(1) What OS you are running?
(2)What compiler and version?
(3) When you do a build would it be possible for you to attach the build log file here?
(4) Is it possible your copy of the input text file is in Unicode format instead of ASCII?
(or not in the correct directory?)
You could add "end=", "err=" and "iostat="to theread statement
and an "err=" and "iostat=" to the open statement.
That would hopefully give you more informationwhen the error occurs.

Better still :
Since this appears to be a sample program and not proprietary, it would help if you could zip up the whole directory and attach the zip file here (like you did for the text file) then we will be able to test it for ourselves.

Les

0 Kudos
Steven_L_Intel1
Employee
745 Views
When you get the breakpoint, look for the console window that was opened when your program ran. It may be hidden behind the debugger window. This will very likely contain the "real" error message, which is possibly an "end of file". The reason is this:

You opened a file called 'res3', but the file is apparently really called res3.txt. Since 'res3' did not exist, Fortran created a new one (it does this unless you add STATUS='OLD' to your OPEN). The new file was empty and the read failed because it hit the end.

I do need to correct John Nichols' comment - you are using list-directed input (the * format) and this will happily read the values from three lines if that's what is needed.

If my understanding of the error is correct, you need to change the filename in the OPEN to 'res3.txt'. If not, please show us the text of the error message from the console window.
0 Kudos
Reply