Fortran读取混合文字和数字 [英] Fortran read mixed text and numbers

查看:110
本文介绍了Fortran读取混合文字和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fortran 90读取包含以下格式数据的文件

I am using Fortran 90 to read a file that contains data in the following format

number# 125 var1= 2 var2= 1 var3: 4
        .
        .
        .
        .
number# 234 var1= 3 var2= 5 var3: 1

我尝试了以下命令,并且运行正常

I tried the following command and works fine

read (2,*)  tempstr , my_param(1), tempstr , my_param(2), tempstr , my_param(3)

问题是当数字变大并且字符串和数字之间没有空格时,即数据看起来如下:

Problem is when the numbers become larger and there is no space between string and number, i.e. the data looks as following:

number# 125 var1= 2 var2=124 var3: 4

我尝试过

     read (2,512)  my_param(1), my_param(2), my_param(3)

512 format('number#', i, 'var1=', i, 'var2=', i, 'var3:', i)

它将所有数字读取为零

我无法切换到其他语言.数据集很大,所以我无法对其进行预处理.同样,分隔符每次都不相同.有人可以帮忙解决这个问题吗?

I can't switch to some other language. The data set is huge, so I can't pre-process it. Also, the delimiters are not the same every time. Can someone please help with the problem?

预先感谢

推荐答案

虽然我仍然支持我的原始答案,特别是因为输入数据已经非常接近名称列表文件的外观,所以我们假设您确实可以不会对数据进行任何预处理.

While I still stand with my original answer, particularly because the input data is already so close to what a namelist file would look like, let's assume that you really can't make any preprocessing of the data beforehand.

下一个最好的方法是将整行读入一个 character(len =< enough>)变量,然后使用String Manipulation提取其中的值.像这样:

The next best thing is to read in the whole line into a character(len=<enough>) variable, then extract the values out of that with String Manipulation. Something like this:

program mixed2
    implicit none
    integer :: num, val1, val2, val3
    character(len=50) :: line
    integer :: io_stat

    open(unit=100, file='data.dat', action='READ', status='OLD')
    do
        read(100, '(A)', iostat=io_stat) line
        if (io_stat /= 0) exit
        call get_values(line, num, val1, val2, val3)
        print *, num, val1, val2, val3
    end do
    close(100)

    contains

        subroutine get_values(line, n, v1, v2, v3)
            implicit none
            character(len=*), intent(in) :: line
            integer, intent(out) :: n, v1, v2, v3
            integer :: idx

            ! Search for "number#"
            idx = index(line, 'number#') + len('number#')

            ! Get the integer after that word
            read(line(idx:idx+3), '(I4)') n

            idx = index(line, 'var1') + len('var1=')
            read(line(idx:idx+3), '(I4)') v1

            idx = index(line, 'var2') + len('var3=')
            read(line(idx:idx+3), '(I4)') v2

            idx = index(line, 'var3') + len('var3:')
            read(line(idx:idx+3), '(I4)') v3
        end subroutine get_values
end program mixed2

请注意,我没有进行任何错误/健全性检查.我会留给你.

Please note that I have not included any error/sanity checking. I'll leave that up to you.

这篇关于Fortran读取混合文字和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆