You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fortran95代码在ifort/ifx中names数组填充异常,其他编译器正常

问题:ifx/ifort编译Fortran95代码时数组填充错误,其他编译器正常

我正在使用Fortran95实现Project Euler第22题的解决方案,该代码在gfortran、nvfortran、flang三款编译器下运行正常,但使用ifx/ifort编译时,名为names的数组填充错误,结果是一堆混乱的内容,包含大量单字符名称和其他无意义的内容。

以下是我的解决方案,open_data_file来自utils模块,临时添加的调试打印语句未做缩进:

! Project Euler Problem 22
!
! Problem:
!
! Using names.txt (right click and 'Save Link/Target As...'), a 46K text file
! containing over five-thousand first names, begin by sorting it into
! alphabetical order. Then working out the alphabetical value for each name,
! multiply this value by its alphabetical position in the list to obtain a name
! score.
!
! For example, when the list is sorted into alphabetical order, COLIN, which is
! worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would
! obtain a score of 938 × 53 = 49714.
!
! What is the total of all the name scores in the file?

module Problem0022
    use constants
    use utils
    implicit none
    integer, parameter :: name_count = 5163
contains
    integer(i18t) function p0022() result(answer)
        character(len=DATA_MAX_NAME_SIZE), parameter :: file_name = "p0022_names.txt"
        character(len=DATA_MAX_NAME_SIZE), dimension(name_count) :: names
        character(len=DATA_MAX_NAME_SIZE) :: temp
        character(len=1) :: current_char
        integer(i18t) :: score
        integer :: ios, unit, i, j, k

        i = 1
        j = 1
        k = 1
        answer = 0
        names = ''
        unit = open_data_file(file_name, .true., 1)
        do
            read(unit, rec=j, IOSTAT=ios) current_char
            j = j + 1
            if (ios /= 0) then
                exit
            end if

            select case (current_char)
                case (',')
                    i = i + 1
                    k = 1
                case ('"')
                case default
                    names(i)(k:k) = current_char
                    k = k + 1
            end select
        end do
do i=1, size(names)
print *, names(i)
end do
        close(unit)
        do i = 1, size(names)
            do j = 1, size(names) - i
                if (names(j) > names(j + 1)) then
                    temp = names(j)
                    names(j) = names(j + 1)
                    names(j + 1) = temp
                end if
            end do
        end do
do i=1, size(names)
print *, names(i)
end do
        close(unit)
        do i = 1, name_count
            score = 0
            do j = 1, len_trim(names(i))
                score = score + ichar(names(i)(j:j)) - ichar('A') + 1
            end do
            answer = answer + score * i
        end do
    end function p0022
end module Problem0022

utils模块中的open_data_file函数代码:

integer function open_data_file(name, direct, recl) result(unit)
        character(len=DATA_MAX_NAME_SIZE), intent(in) :: name
        logical, intent(in), optional :: direct
        integer, intent(in), optional :: recl
        integer :: ios

        unit = prev_unit + 1
        prev_unit = unit
        if (present(direct) .and. present(recl) .and. direct) then
            open(unit=unit, file=("../_data/" // name), status='old', action='read', iostat=ios, access='direct', recl=recl)
        else
            open(unit=unit, file=("../_data/" // name), status='old', action='read', iostat=ios)
        end if
        if (ios /= 0) then
            print *, "Error opening file: ../_data/" // name
            return
        end if
    end function

已尝试的解决步骤:

  • 改为顺序模式读取文件
  • 检查文件编码
  • 调整current_char的类型
  • 添加调试打印语句

但由于环境问题,使用gdb调试Fortran代码时遇到困难,期望ifx/ifort的表现与其他编译器一致。

内容的提问来源于stack exchange,提问作者Olivia A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:48:12