在科学计算和工程应用中,文件操作和数据处理是常见的任务。Fortran提供了强大的文件操作功能,可以方便地读写文件并处理数据。本文将介绍如何在Fortran中进行文件操作,并提供一些实用的示例。
示例1: 从文件读取数据
从文件读取数据
以下代码展示了如何在Fortran中从文件中读取数据。
fortran
复制代码
program read_file
implicit none
integer :: i, n
real :: data(100)
character(len=100) :: filename
print *, 'Enter the name of the file to read:'
read *, filename
open(unit=10, file=filename, status='old', action='read')
i = 0
do
read(10, *, iostat=n) data(i+1)
if (n /= 0) exit
i = i + 1
end do
close(10)
print *, 'Data read from file:'
do n = 1, i
print *, data(n)
end do
end program read_file
代码解释
文件打开: 使用 open 语句打开文件 filename 以读取模式 (status='old',action='read')。读取数据: 使用 read 语句逐行读取文件中的数据并存储在数组 data 中,直到遇到文件结束。文件关闭: 使用 close 语句关闭文件。打印数据: 使用 do 循环打印读取的数据。编译和运行
要编译并运行上述代码,可以使用如下命令:
bash
复制代码
gfortran read_file.f90 -o read_file
./read_file
示例2: 将数据写入文件
将数据写入文件
以下代码展示了如何在Fortran中将数据写入文件。
fortran
复制代码
program write_file
implicit none
integer :: i, n
real :: data(100)
character(len=100) :: filename
print *, 'Enter the name of the file to write:'
read *, filename
print *, 'Enter the number of data points:'
read *, n
! 初始化数据
do i = 1, n
data(i) = real(i) * 1.1
end do
open(unit=20, file=filename, status='unknown', action='write')
do i = 1, n
write(20, *) data(i)
end do
close(20)
print *, 'Data written to file:'
do i = 1, n
print *, data(i)
end do
end program write_file
代码解释
文件打开: 使用 open 语句打开文件 filename 以写入模式 (status='unknown',action='write')。初始化数据: 使用 do 循环初始化数组 data 的元素。写入数据: 使用 write 语句将数组 data 中的数据逐行写入文件。文件关闭: 使用 close 语句关闭文件。打印数据: 使用 do 循环打印写入的数据。编译和运行
要编译并运行上述代码,可以使用如下命令:
bash
复制代码
gfortran write_file.f90 -o write_file
./write_file
示例3: 读取和处理CSV文件
读取和处理CSV文件
以下代码展示了如何在Fortran中读取和处理CSV文件。
fortran
复制代码
program read_csv
implicit none
integer :: i, j, n, m, ios
character(len=100) :: line
character(len=1), parameter :: delimiter = ','
character(len=100), dimension(:), allocatable :: tokens
real, dimension(:,:), allocatable :: data
character(len=100) :: filename
anhui.aaaeq.com/Ji9u8y/
beijing.aaaeq.com/7C3SIH/
chongqing.aaaeq.com/WcZ9lu/
print *, 'Enter the name of the CSV file to read:'
read *, filename
! 打开文件并读取行数和列数
open(unit=10, file=filename, status='old', action='read')
n = 0
m = 0
do
read(10, '(A)', iostat=ios) line
if (ios /= 0) exit
n = n + 1
if (n == 1) then
call count_tokens(line, delimiter, m)
end if
end do
close(10)
! 分配数组
allocate(data(n, m))
allocate(tokens(m))
fujian.aaaeq.com/48iGt2/
gansu.aaaeq.com/as9IIM/
guangdong.aaaeq.com/IVMszF/
! 重新打开文件并读取数据
open(unit=10, file=filename, status='old', action='read')
i = 0
do
read(10, '(A)', iostat=ios) line
if (ios /= 0) exit
i = i + 1
call split_line(line, delimiter, tokens)
do j = 1, m
read(tokens(j), *) data(i, j)
end do
end do
close(10)
! 打印数据
print *, 'Data read from CSV file:'
do i = 1, n
do j = 1, m
print *, 'data(', i, ',', j, ') = ', data(i, j)
end do
end do
contains
subroutine count_tokens(line, delimiter, count)
character(len=*), intent(in) :: line
character(len=*), intent(in) :: delimiter
integer, intent(out) :: count
integer :: i
count = 1
do i = 1, len_trim(line)
if (line(i:i) == delimiter) count = count + 1
end do
end subroutine count_tokens
subroutine split_line(line, delimiter, tokens)
character(len=*), intent(in) :: line
character(len=*), intent(in) :: delimiter
character(len=*), dimension(:), intent(out) :: tokens
integer :: i, start, token_len, token_count
token_count = 1
start = 1
do i = 1, len_trim(line)
if (line(i:i) == delimiter .or. i == len_trim(line)) then
if (i == len_trim(line)) i = i + 1
token_len = i - start
tokens(token_count) = line(start:start+token_len-1)
token_count = token_count + 1
start = i + 1
end if
end do
end subroutine split_line
end program read_csv
代码解释
读取文件行数和列数: 打开CSV文件并读取行数和列数,以便分配数组。分配数组: 根据行数和列数分配数组 data 和 tokens。读取CSV数据: 重新打开CSV文件并逐行读取数据,将每行数据分割成多个token,并将其存储在数组 data 中。打印数据: 使用嵌套的 do 循环打印数组 data 中的CSV数据。