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

查看:17
本文介绍了在 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.

推荐答案

一种方法:将行读入字符串,使用至少与最长预期行一样长的字符串.然后你开始解析字符串.例如,如果数字总是用空格分隔,则使用它来确定子字符串的边界.然后您可以使用内部读取"从每个子字符串中读取以获取数值.内部读取使用字符串而不是单位编号并从字符串中获取数据——至少您不必重新创建字符到数值的转换,读取语句会为您完成.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天全站免登陆