Using do loop in a Fortran 90 program to read different number of lines for n frames?

前端 未结 2 1056
北恋
北恋 2020-12-11 12:04

There is a file that has,say, 1000 frames. Each frame contains different number of lines.Each line has two columns of integers.But,I do not know how many number of lines eac

2条回答
  •  囚心锁ツ
    2020-12-11 12:25

    One of the weaknesses of even modern versions of Fortran (IMO) is the lack of expanding arrays like std::vector in C++ or GArray in GLib/C. Yes, you could write such a thing yourself, but the lack of generics/templates means that you'd have to specialise for every type and kind, or mess about with class(*) and allocatable scalars... urgh.

    But I digress.

    There are two options I can see. The first is to do two passes through the file, first counting the number of lines between each space, and allocating your frame arrays as you go, and the second pass to actually fill the arrays with data.

    The second, possibly more "Fortran-y" way to do it is to have a temporary work array which is as large as you ever expect a single frame to be. Fill this with data, then when you reach the end of a frame, allocate an array of the right size and copy your just-read data into it.

    Neither solution is particularly great though unfortunately.

提交回复
热议问题