简体   繁体   English

使用fortran77将文本文件读取为字符串

[英]Read a text file to a string using fortran77

Is it possible to read a text file to a string using fortran77. 是否可以使用fortran77将文本文件读取为字符串。 I actually have a text file in the following format 我实际上有以下格式的文本文件

Some comments
Some comments

n1  m1  comment_with_unknown_number_of_words
..m1 lines of data..

n2  m2  comment_with_unknown_number_of_words
..m2 lines of data..
and so on

whereas n1,n2.. are the orders of the objects. 而n1,n2 ..是对象的顺序。 m1, m2,..are the number of lines which contains the data about these objects, respectively. m1,m2 ...分别是包含有关这些对象的数据的行数。 I also want to store the comment of each object for further investigations. 我还想存储每个对象的注释以供进一步研究。

How can I deal with this? 我该如何处理? Thank you so much in advance! 提前非常感谢您!

I can't believe nobody called me on this.. My apologies this in fact only grabs the first word of the comment... 我不敢相信没有人给我打电话。.我很抱歉,这实际上只是抓住了评论的第一句话……

------------original answer---- ------------原始答案----

Not to recomend F77, but this isnt that tough a problem either. 不推荐F77,但这也不是难题。 Just declare a char variable long enough to hold your longest comment and use a list directed read. 只需声明一个char变量足够长的时间即可保存最长的注释,并使用直接读取的列表。

integer m1,n1
char*80 comment

...
read(unit,*)m1,n1,comment

If you want to write it back out without padding a bunch of extra spaces thats a bit of effort but hardly the end of the world. 如果您想写出来而又不填很多额外的空间,那会很费力,但是几乎没有世界尽头。

What you can not do at all in f77 is discern whether your file has trailing blanks at the end of a line, unless you go to direct access reading. 在f77中,您根本无法识别出文件的末尾是否有尾随空格,除非您进行直接访问读取。

------------improved answer ------------改进的答案

What you need to do is read the whole line as a string then read your integers from the string: 您需要做的是将整个行作为字符串读取,然后从字符串中读取整数:

read(unit,'(a)')comment
read(comment,*)m1,n1

at this point comment contains the whole line including your two integers (perhaps that will do the job for you). 此时, comment包含整行,其中包括您的两个整数(也许会对您有所帮助)。 If you want to pull off the actual string it requires a bit of coding (I have a ~40 line subroutine to split the string into words). 如果您想提取实际的字符串,则需要一些编码(我有一个〜40行子例程将字符串拆分成单词)。 I could post if interesed but I'm more inclined as others to encourage you to see if your code will work with a more modern compiler. 我可以发表感激之情,但我更倾向于其他人鼓励您查看您的代码是否可以与更现代的编译器一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM