Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an input file with one line formatted like so: 10110100000001011 And I would like to read each digit into an array element using a loop. But if I format it with Read (1, "1i1") num(j) , j =1,10 It only reads the first integer. What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
917 views
Welcome To Ask or Share your Answers For Others

1 Answer

The format 1I1 instructs Fortran to read a single integer from the record/line and then proceed to the next record/line (I mean if that's all that the format contains). If you want to read, e.g., 10 single-digit integers on a single line, then use the format 10I1.

Fortran 2008 adds "unlimited format item" so that you don't have to know the number of items when you write the format: *(i1).

Code example of both methods:

program tst

   integer :: array1 (10), array2 (10)

   open (unit=20, file="digits.txt", access="sequential", form="formatted")

   read (20, '(10i1)' )  array1
   write (*, *) array1

   rewind (20)
   read (20, '( *(i1) )' ) array2
   write (*, *) array2

end program tst

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...