reading text files in FORTRAN - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

reading text files in FORTRAN

Hi, i have a simple question that I know is easy for most of u
I am not an expert programmer but even if u dont know FTN u may gimme a hint on the algorithm and I will figure that out how to do it in FTN

I have a file like

nx=300 ny=12 f=1e4

sad=34.2 nj=23 jb=234

and so on

now I want the code to read 300 and put it into a previous defined integer variable called nx and so on to the end of the file

should I read it character by character ??
how to go to next line??
how to ignore blanks while reading
what size should I use, as each time I may want to for instance, change the value of nx to 200

how to ignore blank lines

I appreciate if somebody can help me

Best,

[email protected]

Comments

  • Malcolm_McLeanMalcolm_McLean Posts: 254Member ✭✭
    It's a complete nuisance, in Fortan 77 at least.
    The short answer is that ypu can't, Fortran's format function only works on fixed length fields. By far the easiest solution will be to pad ypur input file with spaces and then just read in with a format.

    Of course Fortran 77 is a Turing-complete programming language, which means that any computable function can be calculated if you try hard enough. So reading in as a character*1 string and processing character-by character will work (you jump to ypur integer / float prasing subroutine on hitting the first integer, and jump out of it on hitting the first non-number character).
    It is extremely fiddly and almost certainly not worth your while.

Sign In or Register to comment.