用已知行数在Fortran中读取数据文件,但每行中的条目数量未知 [英] Reading data file in Fortran with known number of lines but unknown number of entries in each line

查看:160
本文介绍了用已知行数在Fortran中读取数据文件,但每行中的条目数量未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取包含已知行数的数据文件,但每行中的条目数量未知(例如,如果我的数据文件包含一些像

How can I read the data file containing known number of lines but the number of entries in each line is unknown, e.g. if my data file contain some thing like


1 3 4 5 6 -7 8 -9

1 3 4 5 6 -7 8 -9

1 3 5 6

4 5 6 7 8 3 5 6 7 8 4 5 7 8

4 5 6 7 8 3 5 6 7 8 4 5 7 8

即三行,但每行中的数据是未知的。有一次我需要从一行中获取数据。

i.e. three lines but the data in each line is unknown. At one time I need the data from one line.

推荐答案

一种方法:使用至少与最长期望行一样长的字符串将行读入字符串。然后你去解析字符串。例如,如果数字总是以空格分隔,则使用它来计算子字符串边界。然后,您可以使用内部读取从每个子字符串中读取数值。内部读取使用字符串而不是单位号码,并从字符串中获取数据 - 至少您不必重新创建字符到数值的转换,read语句将为您执行此操作。 Fortran提供的内部函数将使解析更加容易。

One method: read the line into a string, using a string that is at least as long as the longest expected line. Then you go about parsing the string. E.g., if the numbers are always split by spaces, use that to figure out the substring boundaries. Then you can use "internal reads" to read from each sub-string to obtain the numeric values. An internal read uses a string instead of a unit number and obtains the data from the string -- at least you don't have to recreate the conversion of characters to numeric values, the read statement will do that for you. The intrinsic functions provided with Fortran will make the parsing easier.

这篇关于用已知行数在Fortran中读取数据文件,但每行中的条目数量未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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