readfile - How do I read in a consecutive row of a text file in each step of a do-loop in fortran? -
i have dataset of parameter values 30 species, , want run script conducts simulation each species. parameter values stored in .txt file, each row different species, , each column different parameter value. i'd set do-loop reads in relevant row of parameter values each species, runs simulation script, , writes .txt file of output each species. unfortunately, i'm new fortran , having lot of trouble understanding how read in consecutive rows .txt file in each step of loop. tried making simplified script test whether read step working:
program driver implicit none integer :: mm ! forgot line in first version of question , edited add in character(7) :: species !! first column species name real*8 :: leaf_variable ! next 3 columns variable values real*8 :: stem_variable ! real*8 :: root_variable ! open (12, file = "species_parameters.txt") ! open .txt file mm = 1,30 ! set loop read (12,*) species, leaf_variable, stem_variable, root_variable ! read in species-specific parameter values write (*,*) species, leaf_variable, stem_variable, root_variable ! print values on screen show loop runs enddo end program driver
but when go compile, error: @ line xx of file xx (unit = 12, file = 'species_parameters.txt') fortran runtime error: end of file
what misunderstanding opening , reading in file?
thanks help.
edit: think i've narrowed down problem. understanding read() takes in 1 row in .txt file @ time, in example:
read(7, *) species, leaf_variable, stem_variable, root_variable read(7, *) species, leaf_variable, stem_variable, root_variable
the variables should equal values in second row of .txt file. instead, no matter how many times put in read() function, variable values equal first row. and, though there 4 columns, can define many variables want read() function:
read(7, *) species, leaf_variable, stem_variable, root_variable, fake_variable1, fake_variable2, fake_variable3, fake_variable4
where fake_variable values equal values in second row of .txt file. confused read() does, or there need keep script reading entire .txt file 1 line?
edit #2: loop reads line line correctly i've saved .txt file unix encoding using textwrangler. original file saved .txt file excel. seems have solved it, if has suggestions better way specify input file format, i'd appreciate it. first few lines of input file this:
species1,1.2,6.54,10.9 species2,1.42,3.5,8.23 species3,0.85,2.41,4.9
a run time error when have executable, execute it, , crashes. compile time error when compiler fails produce executable.
this code shouldn't compile, because have implicit none
, haven't declared integer mm
.
what i'd recommend more information:
program driver use iso_fortran_env implicit none character(len=7) :: species real(kind=real64) :: leaf_variable, stem_variable, root_variable integer :: u, ioerr character(len=120) :: iomsg open(newunit=u, file='species_parameters.txt', action='read', status='old', iostat=ioerr, iomsg=iomsg) if (ioerr /= 0) print *, "error opening file" print *, trim(iomsg) stop 1 end if read(u, *, iostat=ioerr, iomsg=iomsg) species, leaf_variable, stem_variable, root_variable if (ioerr /= 0) exit ! exits loop write(*, *) species, leaf_variable, stem_variable, root_variable end print *, trim(iomsg) close(u) end program driver
this print "read past end of file" error, check how program reads anyway.
this should compile, , when run it, should give information on going wrong.
Comments
Post a Comment