fortran实战手册(3)

发布时间 2023-11-19 00:05:13作者: 水宝石

read,write

读写文件

program learn
    implicit none
    character(10)::student_name
    character(20)::file_name
    integer::id
    integer::io_status
    character(100)::err_msg
    real::math_score,english_score,politics_score

    write(*,*) "请输入文件名:"
    read (*,*)  file_name
    write(*,*) "请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):"
    read (*,*) id,student_name,math_score,english_score,politics_score

    open (unit=11,file=file_name,status='replace',action='write',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "写入文件",file_name,"异常"
    else
        write (11,100) id,student_name,math_score,english_score,politics_score
        close(unit=11)
    endif


    open (unit=10,file=file_name,status='old',action='read',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "读取文件",file_name,"异常"
    else
        read (10,100) id,student_name,math_score,english_score,politics_score
        write (*,100) id,student_name,math_score,english_score,politics_score
        close(unit=10)
    endif
    100 format(I5,A10,3F6.2)


end program learn