Read file in fortran

  • Fortran
  • Thread starter marlh
  • Start date
  • #1
12
0

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
DrClaude
Mentor
7,148
3,283
Quick and dirty solution:
Code:
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:
gfortran -O3 -o readdata readdata.f90
 
  • #3
12
0
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).
 

Related Threads on Read file in fortran

Replies
1
Views
5K
Replies
1
Views
15K
Replies
4
Views
10K
Replies
12
Views
13K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
7
Views
1K
Replies
5
Views
3K
Replies
5
Views
17K
Replies
2
Views
2K
Top