在Ubuntu下使用Fortran进行数据输入输出,你可以使用内置的I/O语句和函数。以下是一些基本的示例:
program read_input
implicit none
integer :: num
real :: pi_value
print *, 'Enter an integer:'
read *, num
print *, 'Enter a floating-point number:'
read *, pi_value
print *, 'You entered:', num, 'and', pi_value
end program read_input
program read_from_file
implicit none
integer :: iounit, ios
real, allocatable :: data(:)
! 打开文件
open(unit=iounit, file='data.txt', status='old', action='read', iostat=ios)
if (ios /= 0) then
print *, 'Error opening file!'
stop
end if
! 读取数据
read(iounit, *) data
! 关闭文件
close(iounit)
print *, 'Data read from file:', data
end program read_from_file
program write_output
implicit none
integer :: num = 10
real :: pi_value = 3.14159
print *, 'The integer is:', num
print *, 'The value of pi is:', pi_value
end program write_output
program write_to_file
implicit none
integer, parameter :: iounit = 10
real, dimension(5) :: data = [1.0, 2.0, 3.0, 4.0, 5.0]
! 打开文件
open(unit=iounit, file='output.txt', status='replace', action='write')
! 写入数据
write(iounit, *) 'Data to be written to the file:', data
! 关闭文件
close(iounit)
print *, 'Data written to file successfully!'
end program write_to_file
iostat参数来检查I/O操作是否成功。old、new、replace等)。通过这些基本的示例,你应该能够在Ubuntu下使用Fortran进行简单的数据输入输出操作。如果你有更复杂的需求,可以进一步学习Fortran的I/O语句和函数。