Fortran Program to Read Data from 'wind.txt' File
! This program reads data from a file called 'wind.txt'
! The file contains three columns of data: x, y, and z coordinates
! The program reads the number of rows in the file first, and then allocates
! three arrays of that size to store the data
! Finally, the program prints out the arrays for verification
program read_wind_file
implicit none
integer :: i, n
real :: x, y, z
real, dimension(:), allocatable :: col1, col2, col3
! open the file for reading
open(10, file='wind.txt')
n = 0 ! initialize the row count to zero
! loop through the file to count the number of rows
do while (.true.)
read(10,*,iostat=i) x, y, z ! read a row of data
if (i/=0) exit ! check if end of file or error
n = n + 1 ! increment the row count
end do
! allocate arrays to store the data
allocate(col1(n), col2(n), col3(n))
rewind(10) ! move the file pointer back to the beginning of the file
! loop through the file again to read the data into arrays
do i=1,n
read(10,*) col1(i), col2(i), col3(i)
end do
close(10) ! close the file
! print out the arrays for verification
do i=1,n
write(*,*) col1(i), col2(i), col3(i)
end do
end program read_wind_file
原文地址: https://www.cveoy.top/t/topic/nF5S 著作权归作者所有。请勿转载和采集!