在网上找了个能读取当前文件夹中文件名的程序,我想把读取的文件输出到name.txt中,但是每次运行都只能得到最后一个文件。
比如当前文件夹下有1.txt,2.txt,运行程序后,屏幕显示两个文件都在,但是在name.txt中就只有2.txt。
想请问下面程序中的输出部分应如何修改。
能给些建议也好哦,有时间加Q271974603多多交流。!输出文件名文件——name.txt
Program output_file_name
        Implicit None
        External WriteName
        Integer::n
        Call DoWithWildcard('*.*',WriteName,n)
        If(n>=0)then
                Write(*,*)'共',n,'个文件!'
        End If
End Program output_file_name


Subroutine WriteName(FileName,loop )
        Character(*),Intent(In)::FileName
        Integer,Intent(In)::loop
        Write(*,*)FileName
        open(1,file='name.txt')
        write(1,*)FileName
        close(1)
End Subroutine WriteName


Subroutine DoWithWildcard(cWildcard,CallBack,iTotal)
        Use DFLib,only:GetFileInfoQQ,GetLastErrorQQ,FILE$INFO,FILE$LAST,FILE$ERROR,FILE$FIRST,ERR$NOMEM,ERR$NOENT,FILE$DIR
        Implicit None
        Interface 
                Subroutine CallBack(FileName,loop)
                        Character(*),Intent(In)::FileName
                        Integer,Intent(In)::loop
                End Subroutine CallBack
        End Interface
        Character*(*),Intent(In)::cWildcard
        Integer,Intent(Out)::iTotal
        TYPE (FILE$INFO) info
        INTEGER(4)::Wildhandle,length,retInt
        Wildhandle=FILE$FIRST
        iTotal=0
        DO WHILE (.TRUE.)
                length=GetFileInfoQQ(cWildCard,info,Wildhandle)
                IF((Wildhandle.EQ.FILE$LAST).OR.(Wildhandle.EQ.FILE$ERROR))THEN
                        SELECT CASE(GetLastErrorQQ())
                                CASE(ERR$NOMEM)
                                        iTotal=- 1
                                        Return
                                CASE(ERR$NOENT)
                                        Return
                                CASE DEFAULT
                                        iTotal=0
                                        Return
                        END SELECT
                END IF
                If((info%permit.AND.FILE$DIR).Eq.0)then
                        Call CallBack(Trim(info.Name), iTotal+1 )
                        iTotal=iTotal+1
                End If
        END DO
End Subroutine DoWithWildcard