Fortran:一次从一行读取一个值

发布于 2024-12-06 22:49:18 字数 209 浏览 0 评论 0原文

我正在使用 FORTRAN 从 ASCII 文本文件中读取数据。该文件每行包含多个数据值,但每行值的数量不是恒定的。

101.5 201.6 21.4 2145.5
45.6 21.2
478.5
...

通常,在读语句之后,Fortran 会转到下一行。我想要做的是一次读取一个数据值。如果它到达行尾,它应该继续阅读下一行。这可能吗?

I am using FORTRAN to read in data from an ASCII text file. The file contains multiple data values per line but the number of values per line is not constant.

101.5 201.6 21.4 2145.5
45.6 21.2
478.5
...

Normally after a read statement, Fortran would go to the next line. What I want to be able to do is read one data value at a time. If it hits the end of the line, it should just continue reading on the next line. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2024-12-13 22:49:18

正如 IRO-bot 在对您的问题的评论中指出的那样,MSB 已经给出了答案,下面我仅提供了一些说明该答案的代码(因为 MSB 的帖子不包含任何代码):

program test

   character(len=40) :: line
   integer           :: success, i, indx, prev, beginning
   real              :: value

   open(1,file='test.txt')

   do  
      read(1,'(A)',iostat=success) line
      if (success.ne.0) exit

      prev      = 1 
      beginning = 1 

      do i=1,len(line)

         ! is the current character one out of the desired set? (if you
         ! have got negative numbers as well, don't forget to add a '-')
         indx = index('0123456789.', line(i:i))

         ! store value when you have reached a blank (or any other 
         ! non-real number character)
         if (indx.eq.0 .and. prev.gt.0) then
            read(line(beginning:i-1), *) value
            print *, value
         else if (indx.gt.0 .and. prev.eq.0) then
            beginning = i 
         end if

         prev = indx
      end do
   end do

   close(1)

end program test

使用您提供的示例行运行此程序时输出是

101.5000    
201.6000    
21.40000    
2145.500    
45.60000    
21.20000    
478.5000

我希望你会发现这有帮助。

As pointed out by IRO-bot in their comment to your question, the answer has already been given by M.S.B. Below I have merely provided some code illustrating that answer (as M.S.B.'s post contained none):

program test

   character(len=40) :: line
   integer           :: success, i, indx, prev, beginning
   real              :: value

   open(1,file='test.txt')

   do  
      read(1,'(A)',iostat=success) line
      if (success.ne.0) exit

      prev      = 1 
      beginning = 1 

      do i=1,len(line)

         ! is the current character one out of the desired set? (if you
         ! have got negative numbers as well, don't forget to add a '-')
         indx = index('0123456789.', line(i:i))

         ! store value when you have reached a blank (or any other 
         ! non-real number character)
         if (indx.eq.0 .and. prev.gt.0) then
            read(line(beginning:i-1), *) value
            print *, value
         else if (indx.gt.0 .and. prev.eq.0) then
            beginning = i 
         end if

         prev = indx
      end do
   end do

   close(1)

end program test

When running this program using the sample lines you provided the output is

101.5000    
201.6000    
21.40000    
2145.500    
45.60000    
21.20000    
478.5000

I hope you will find this helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文