- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
A development in my code has left me with the following "simple" INTEGER input question. A single line in a text file has either one or two integer values, but a priori I do not know which. How might I read this line and determine how many inputs?
My attempt was the following:
I2 = -1 READ(IOUNIT, *, ERR = 11) I1, I2 11 CONTINUE IF (I2 == -1) THEN ! Do something appropriate to initialize I2 END IF
This fails because the error may have been triggered, but the READ statement is now gobbling more input. What is the "best" way to do this?
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
And for your follow-up question:
program read_x_y implicit none integer, parameter :: MaxInputLine=128 character(len=MaxInputLine) :: InputLine integer, parameter :: N = 10 integer :: Array(N) integer :: comma, i read(*,'(A)') InputLine Array = -1 do i=1,N if(InputLine == ' ') exit read(InputLine,*) Array(i) comma = index(InputLine,',') if(comma <= 0) exit InputLine = InputLine(comma+1:) end do print *, Array end program read_x_y
Jim Dempsey
コピーされたリンク
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
Note: I am using Fortran 2003 as the standard for my code, so non-advancing input with appropriate settings is a viable option.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
program read_x_y implicit none integer, parameter :: MaxInputLine=128 character(len=MaxInputLine) :: InputLine integer :: i1, i2,comma read(*,'(A)') InputLine read(InputLine,*) i1 i2=-1 comma = index(InputLine,',') if(comma > 0) read(InputLine(comma+1:),*) i2 print *, i1,i2 end program read_x_y
The above is one way to do this. You may want to improve the delimiter detection (space, tab, accept and evaluate an expression, etc...)
Jim Dempsey
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
And for your follow-up question:
program read_x_y implicit none integer, parameter :: MaxInputLine=128 character(len=MaxInputLine) :: InputLine integer, parameter :: N = 10 integer :: Array(N) integer :: comma, i read(*,'(A)') InputLine Array = -1 do i=1,N if(InputLine == ' ') exit read(InputLine,*) Array(i) comma = index(InputLine,',') if(comma <= 0) exit InputLine = InputLine(comma+1:) end do print *, Array end program read_x_y
Jim Dempsey
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
Thank you. I had done some Googling and found a similar solution on StackOverflow. It is working, and yours is more generic. I will be implementing as such.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
You might want to look on this forum a little more. Someone posted a really good input parser.
https://software.intel.com/pt-br/comment/1478224
There are more.
Jim Dempsey