Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Read file in fortran

  1. Sep 30, 2013 #1
    I'm trying to read the next data text file :cross.dat

    1.000000-5 0.000000+0 2.530000-2 0.000000+0 7.712958+1 0.000000+0
    2.250000+3 0.000000+0 2.250000+3 1.838880+1 2.300000+3 1.936710+1
    2.500000+3 1.986000+1 2.650000+3 1.843220+1 2.900000+3 1.832230+1
    3.000000+3 1.816280+1 3.150000+3 1.834270+1 3.250000+3 1.872690+1

    (datatable is sigma form nndc.bnl.gov)
    then, i write them in new file: rewritecross.dat

    1.000000-5 0.000000+0
    2.530000-2 0.000000+0
    7.712958+1 0.000000+0
    2.250000+3 0.000000+0
    2.250000+3 1.838880+1
    2.300000+3 1.936710+1
    2.500000+3 1.986000+1
    2.650000+3 1.843220+1
    2.900000+3 1.832230+1
    3.000000+3 1.816280+1
    3.150000+3 1.834270+1
    3.250000+3 1.872690+1

    Please, help me solution it. Thanks.
     
  2. jcsd
  3. Sep 30, 2013 #2

    DrClaude

    User Avatar

    Staff: Mentor

    Quick and dirty solution:
    Code (Text):
    program readdata

      implicit none
      integer :: i
      real :: x(6)

      open(10,file="cross.dat",status="old")
      open(11,file="cross_new.dat")

      do i = 1,4
         read(10,*) x
         write(11,99) x(1:2)
         write(11,99) x(3:4)
         write(11,99) x(5:6)
    99 format(e12.7,1x,e12.7)
      end do

      close(10)
      close(11)


    end program readdata
     
    Compile with
    Code (Text):
    gfortran -O3 -o readdata readdata.f90
     
     
  4. Sep 30, 2013 #3
    Thanks PF Patron!
    That's nice solution. I have one more question about format: do you know how to remove letter 'E' in result (This looks like 1.000000-5 replace 1.000000E-5).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook