Read File in Fortran & Rewritecross.dat

  • Context: Fortran 
  • Thread starter Thread starter marlh
  • Start date Start date
  • Tags Tags
    File Fortran
Click For Summary
SUMMARY

This discussion focuses on reading and rewriting data from a text file in Fortran, specifically using the file "cross.dat" and outputting to "rewritecross.dat". The provided Fortran code utilizes the gfortran compiler with optimization flag -O3 to read data in sigma format and write it in a specified format. Additionally, a question arises regarding the removal of the letter 'E' from the scientific notation in the output, indicating a need for formatting adjustments in the Fortran code.

PREREQUISITES
  • Familiarity with Fortran programming language
  • Understanding of file I/O operations in Fortran
  • Knowledge of scientific notation and formatting in Fortran
  • Experience with gfortran compiler and its optimization flags
NEXT STEPS
  • Learn Fortran formatted output techniques to customize data representation
  • Explore advanced file handling in Fortran for larger datasets
  • Investigate the use of Fortran modules for better code organization
  • Research optimization techniques in Fortran for performance enhancement
USEFUL FOR

This discussion is beneficial for Fortran developers, data scientists working with scientific data, and anyone involved in data processing and formatting in Fortran.

marlh
Messages
12
Reaction score
0
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.
 
Technology news on Phys.org
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
 
  • Like
Likes   Reactions: 1 person
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).
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K