Fortran读取一行字符串中的数字

利用Fortran中文件的基本操作来读取每行字符串中的数字内容,测试数据如下:

xxx2009
xx xxxx 2013
2014i love you 1
big2015more2016
ilove 2017 good 3
big2015more2018

运行结果为:

2009
2013
20141
20152016
20173016
20152018

代码如下:

Program main !读取每行中的数字
    implicit none
    character(len=128) line
    character(len=20) numstr
    integer length,i,k,flag
    open(10,file="data.txt")
    open(11,file="transfor.txt")
    flag=0
    do while(.not.eof(10))
        read(10,'(a)') line
        length=len_trim(line)
        k=0
        do i=1,length
            flag=ichar(line(i:i))
            if((flag.le.ichar('9')).and.(flag.ge.ichar('0'))) then
                k=k+1
                numstr(k:k)=line(i:i)
            endif
        enddo
        write(11,*) numstr 
    enddo
    close(10)
    close(11)
    stop
    end


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章