在Ubuntu上进行Fortran的输入输出操作,你需要使用Fortran的标准库函数。以下是一些基本的输入输出操作的示例:
program print_example
    implicit none
    print *, 'Hello, World!'
end program print_example
在这个例子中,print *语句用于打印字符串'Hello, World!'到标准输出。
program read_example
    implicit none
    character(len=100) :: name
    print *, 'What is your name?'
    read *, name
    print *, 'Hello,', name, '!'
end program read_example
这个程序会提示用户输入他们的名字,并将其存储在变量name中,然后打印一个问候语。
Fortran也支持文件的读写操作。以下是一个简单的例子,演示如何写入和读取文件:
program write_to_file
    implicit none
    integer :: iounit, i
    real, dimension(5) :: data = [1.0, 2.0, 3.0, 4.0, 5.0]
    ! 获取一个未使用的文件单元号
    open(newunit=iounit, file='data.txt', status='replace')
    ! 写入数据到文件
    do i = 1, 5
        write(iounit, *) data(i)
    end do
    ! 关闭文件
    close(iounit)
end program write_to_file
program read_from_file
    implicit none
    integer :: iounit, i
    real, dimension(5) :: data
    ! 获取一个未使用的文件单元号
    open(newunit=iounit, file='data.txt', status='old')
    ! 从文件读取数据
    do i = 1, 5
        read(iounit, *) data(i)
    end do
    ! 关闭文件
    close(iounit)
    ! 打印读取的数据
    print *, 'Data read from file:', data
end program read_from_file
在这些例子中,open语句用于打开文件,write语句用于写入数据,read语句用于读取数据,close语句用于关闭文件。newunit关键字用于自动分配一个未使用的文件单元号。
在Ubuntu上,你可以使用gfortran编译器来编译Fortran程序。例如,如果你有一个名为example.f90的Fortran源文件,你可以使用以下命令来编译它:
gfortran -o example example.f90
然后,你可以运行编译后的程序:
./example
确保你的Fortran代码遵循正确的语法和逻辑,以便顺利执行输入输出操作。