Reading files into fortran

不羁的心 提交于 2019-12-20 05:44:11

问题


So I'm writing some code in Fortran that multiplies a square matrix by itself. But the matrix I have to multiply is in a file and I'm having some issues reading it into the program. I think its because the sample data is in the following format:

3

101

010

101

The first row is the dimension of the matrix, and each row is a now in the matrix, but there aren't spaces in between the entries. So I guess my question is how do I split up those rows as I read them into a 2d array?


回答1:


Read in the first number as N and use it to allocate an array of dimension N by N. Then read a row at a time of this array: array (i, 1:N)) for i=1 to N. See Fortran: reading a row of numbers into an array for the format to use.




回答2:


Read using format

read (1,*) n
allocate(A(n,n))
do i=1,n
  read (1,'(1000i1)'),A(i,:)
enddo

it does not matter whether you declare extra "i1" than actually needed



来源:https://stackoverflow.com/questions/15013561/reading-files-into-fortran

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