使用 Do-Loop 读取一列数据(数字和字符串)并将数字过滤为输出到另一个文件中
我有一个输出文件,单列,其中每个第七行是一个字符串,其他行是数字(如下所示)
998.69733
377.29340
142.22397
53.198547
19.743515
7.5493960
timestep: 1
998.69733
377.29340
142.22047
53.188023
19.755905
7.5060229
timestep: 2
998.69733
377.29337
我需要将此数据读入另一个文件,省略文本并仅保留数字,并尝试循环来分配我的字符串的虚拟,但我收到错误,因为它无法识别(AI)。
DO 10 I = 1, 1000
IF (MOD(I,7) == 0) THEN
READ (8, FMT= '(AI)') dummy
END IF
READ (8,*) val
WRITE (9,*) val
10 CONTINUE
(8 - 输入文件和 9 - 输出文件分配)
我对 Fortran 很陌生,花了很多时间寻找解决方案或至少是类似的问题,但没有找到任何东西。我真的很感激一些帮助。
预先非常感谢您。
I have an output file, single column, where each 7th line is a string and the others numerical (something like below)
998.69733
377.29340
142.22397
53.198547
19.743515
7.5493960
timestep: 1
998.69733
377.29340
142.22047
53.188023
19.755905
7.5060229
timestep: 2
998.69733
377.29337
I need to read this data into another file, omitting the text and keeping only the numbers and tried a loop to allocate a dummy for my string but I get an error as it does not recognize (AI).
DO 10 I = 1, 1000
IF (MOD(I,7) == 0) THEN
READ (8, FMT= '(AI)') dummy
END IF
READ (8,*) val
WRITE (9,*) val
10 CONTINUE
(8 - input file and 9 - output file allocation)
I am quite new to Fortran and spent a lot of time surfing for a solution or at least a similar problem but did not find anything. I would really appreciate some help.
Thank you very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想跳过第七行,您可以执行“read (8, '(A)' ) dummy”,其中 dummy 被声明为字符串(即“character (len=80) :: dummy”)。有些字符是字母而其他字符是数字并不重要。
PS 现代的循环编写方式是“do”、“end do”……不需要行号和 continue 语句。
If you just want to skip the seventh lines, you could do "read (8, '(A)' ) dummy" where dummy is declared as a character string (i.e., "character (len=80) :: dummy"). It won't matter than some of the characters are letters and others numbers.
P.S. The modern was to write loops is "do", "end do" ... no need for line numbers and continue statements.
只需使用带有空输入项列表的列表定向输入即可。
另外,第七行在循环中被读取两次。将
val
的读写放在ELSE
部分中,或者使用CYCLE
语句:Simply use list-directed input with an empty input item list.
Also, the seventh line gets read twice in your loop. Put the read and write of
val
in anELSE
section, or, alternatively, us theCYCLE
statement: