How to read comma delimited data file if some data include spaces

China☆狼群 提交于 2019-12-04 19:59:31

One possibility would be to read in the entire line into a string buffer, and look for (some of) the delimiters yourself. Assuming that similar to your example, only the first column contains with whitespaces, you could do like:

program test
  implicit none

  character(1024) :: buffer
  character(20) :: var1
  integer :: pos, var2
  real :: var3, var4

  read(*,"(A)") buffer
  pos = index(buffer, ",")
  var1 = buffer(1:pos-1)
  read(buffer(pos+1:), *) var2, var3, var4
  print *, var1, var2, var3, var4

end program test

This way, you split that part of the string manually which is affected by the spaces, and everything else after it you conviniently read via the read statement. If not just the first but also other fields can contain whitespaces, it is easy to extend the example above to look for all the necessary delimiters in the buffer via the index() function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!