1

Good evening! I am trying to read a text document in Fortran 95 and do some calculations with the values in it. The document has numerous gas names and certain values of 'A' assigned to them. So essentially it look something like this:

Document 1: gas values.
GAS     A1  A2
steam    1  2
air      3  4

I want then the user to input a gas name (variable gasNameIn) and implement while loop to keep searching for the gas until it matches the input. So eg. user inputs 'air' and the program starts reading first words until air comes up. It then read values of A1 and A2 and uses them for calculation. What I did for it is that I opened the file as unit 25 and tried the following loop:

do while(gasName .NE. gasNameIn)
  read(25, *) gasName
  if (gasName .EQ. gasNameIn)
    read(25,*) A1, A2
  endif
enddo

but I get an error "End of file on unit 25". Any ideas on how my while loop is wrong? Thank you!

3 Answers 3

0

By the first read statement, you read the name correctly, but Fortran then proceeds to the next line (record). Try to use

  read(25, *, advance='no') gasName

If your searched gas was on the last line, you get the end of file error. Otherwise you will have an error when reading A1.

2
  • Note that use of list directed formatting and an ADVANCE specifier in an input/output statement violates a language constraint.
    – IanH
    Nov 29, 2012 at 0:39
  • Of course, I just editted the OP's line quickly. No problem to change to a. Nov 29, 2012 at 8:11
0

you need to read whole lines as strings and process. This is untested but the jist of it:

character*100 wholeline
wholeline=''
do while(index(wholeline,'air').eq.0)
read(unit,'(a)')wholeline
end do

then if you can count on the the first strings taking up ~7 cols like in the example,

read(wholeline(7:),*)ia1,ia2
0

What happened is that you read the whole line in as "gasName" and tested the whole line to see if it was equivalent to "gasNameIn". It never will be the same if the data is laid out the way you have in your sample, so you will get to the end of your file before you ever get a match. It has been a while since I've written in Fortran, but in the first loop "gasName" will be undefined. Usually that is a no no in programming.

The answer that got in before I could get mine typed in is the way forward with your problem. Give it a go. If you still have some trouble maybe I'll fire up a Fortran compiler and try my hand at Fortran again. (It's been since the early 90's that I've done any.)

CHEERS!

Hi, here is your code modified a bit, you can pitch what you don't need. I just added a couple of lines to make it a self-contained program. For instance, There is a statement which assigns the value "steam" to "gasNameIn". I think you mentioned that you would have the user enter a value for that. I've also added a "write" statement to show that your program has properly read the values. It is a crude kind of "unit test". Otherwise, I've only addressed the part of the program that your question asked about. Check this out:

character (len=5) :: gasName, gasNameIn

gasNameIn = "steam"

open (unit = 25, file = "gasvalues.txt")

do while (gasName .NE. gasNameIn)
   read (25, *) gasName
   if (gasName .EQ. gasNameIn) then
   backspace (25)   
       read (25,*) gasName, A1, A2
       write (*, *) "The gas is: ", gasName, ". The factors are A1: ", A1, "; A2: ", A2
   endif
end do

close (25)

end

Ok, here are a couple of notes to help clarify what I've done. I tried to keep your code as intact as I could manage. In your program there were some things that my fortran 95 compiler complained about namely: the "then" is required in the "if" test, and "enddo" needed to be "end do". I think those were the main compiler issues I had.

As far as your code goes, when your program tests that "gasName" is the same as "gasNameIn", then you want to "backup" to that record again so that you can re-read the line. There are probably better ways to do this, but I tried to keep your program ideas the way you wanted them. Oh, yes, one more thing, I've named the input file "gasvalues.txt".

I hope this helps some. CHEERS!

2
  • I'm sorry, but I don't seem to be able to see the other answer. Can you give me a brief summary of what he was saying there?
    – Gleko
    Nov 26, 2012 at 21:11
  • I've just set up gfortran on my mac now, so have another look at my edited answer, I hope it helps. Nov 27, 2012 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.