Read File in Fortran & Rewritecross.dat

In summary, the conversation is about reading a data text file and rewriting it into a new file. The format of the data table is provided and a quick and dirty solution is suggested using a Fortran program. The program opens the original file, reads the data, and writes it into the new file with the desired format. The use of "-O3" in the compilation removes the letter "E" in the result.
  • #1
marlh
12
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
  • #2
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 1 person
  • #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).
 

1. What is the purpose of reading a file in Fortran?

Reading a file in Fortran allows a program to access data or information stored in an external file, making it possible to manipulate and analyze large amounts of data efficiently.

2. How do I read a file in Fortran?

To read a file in Fortran, you can use the OPEN, READ, and CLOSE statements. First, the file must be opened using the OPEN statement, then data can be read from the file using the READ statement, and finally the file should be closed using the CLOSE statement.

3. What is the format for reading data from a file in Fortran?

The format for reading data from a file in Fortran depends on the type and structure of the data in the file. The READ statement should specify the data type and the order in which the data is stored in the file.

4. How do I handle errors when reading a file in Fortran?

To handle errors when reading a file in Fortran, you can use the IOSTAT or ERR keyword in the READ statement. These keywords will return a value indicating if an error occurred while reading the data from the file.

5. Can I rewrite the data in a file using Fortran?

Yes, you can rewrite data in a file using Fortran by using the WRITE statement. This statement allows you to write data to a file in a specified format. However, it is important to note that this will overwrite any existing data in the file.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
615
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top