Skip a line from a text file in Fortran90

I write in fortran (90). My program should read file1, do something with each line and write the result to file2. But the problem is, file1 has some unnecessary information in the first line.

How can I skip a line from an input file using Fortran?

The code:

open (18, file='m3dv.dat') open (19, file='m3dv2.dat') do read(18,*) x tmp = sqrt(x**2 + 1) write(19, *) tmp end do 

The first line is a combination of text and numbers.

+6
source share
3 answers

You have already found a solution, but I just wanted to add that you do not even need a dummy variable , just an empty read statement before entering the loop is enough:

 open(18, file='m3dv.dat') read(18,*) do ... 

Other answers are correct, but this can improve the brevity and (thus) the readability of your code.

+11
source

Perform a read operation before the do loop, which reads everything on the first line into the "dummy" variable.

 program linereadtest implicit none character (LEN=75) ::firstline integer :: temp,n ! ! ! open(18,file='linereadtest.txt') read(18,*) firstline do n=1,4 read(18,'(i3)') temp write(*,*) temp end do stop end program linereadtest 

Datafile:

This is a test of 1000 things, 10 of which do not exist

 50 100 34 566 

! ignore the space between the string and the numbers, I can not get it to format

+1
source
 open (18, file='m3dv.dat') open (19, file='m3dv2.dat') read(18,*) x // <--- do read(18,*) x tmp = sqrt(x**2 + 1) write(19, *) tmp end do 

The added line simply reads the first line and then overwrites it second in the first iteration.

0
source

Source: https://habr.com/ru/post/885604/


All Articles