Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29274 Discussions

Read data file: letters, numbers, blank lines

gabams
Beginner
938 Views
Hi everybody,

I would like to read a data file that has letters, numbers and blank lines in it. I know that it has 3 columns, however I don't know the number of lines. And it looks like :

aaa b ccc
0 1 2
3 4 5
6 7 7

dd eee ffff
8 9 10
11 12 13

ggg hh i
14 15 16
17 18 19
20 21 22
23 24 25

So it begins with a line of letters (which don't interest me), then I have several lines of numbers (which I'd like to put in an array), then I have a blank line, and then again a line of letters, several lines of numbers, a blank line,...

What I would like to do when I read the file, is to find the line of letters, discard it, put the numbers in an array. Then comes the blank line, which announces a new line of letters, and I don't take care of these two lines, but then I put the next numbers in a new array (or in the same array, but along a different dimension), and so on until the end of the file.

What I thought doing was finding the blank lines, saying "jump the next line", and then putting my numbers in an array. Is there a way of doing this? Or would you think of something simpler?

Thanks a lot for your help!

Gabams
0 Kudos
2 Replies
anthonyrichards
New Contributor III
938 Views
Open the file with FORM='FORMATTED'. e.g.
OPEN(100,FILE='MYFILE.TXT',FORM='FORMATTED',STATUS='OLD')
Start a READ loop, for example
1000 read(100,*, ERR=9998, END=9999) STRING
.....
..... (yourcode goes here)
go to 1000
9998 CONTINUE
(output some message about error reading file)
CALL EXIT(-1)
9999 continue
(output some message aboutend of data file reached)
CALL EXIT(0)
(or rest of code)
Readeach record intothe character variable, STRING say,dimensioned big enough to take the longestrecord you expect.
Each time, establish the length,LSTRING,of the record by using the intrinsic function LEN_TRIM.
LENGTH=LEN_TRIM(STRING)
If the length is zero, loop to next record.
If the LENGTH is non-zero, examine the ASCII code of the first character using ICHAR(STRING1:1))
A decimal digit , 0 to 9, will have a decimal ASCII code value between 48 (0) and 57 (9)inclusive.
Once you have found a decimal digit, read the three numbers in the STRING using an internal READ and list-directed format
ICOUNT=ICOUNT+1
READ(STRING(1:LSTRING),*) (IARRAY(ICOUNT,J),J=1,3)
Then loop back to read the next record in the file.
The counter ICOUNT keeps track of which record has been read in, counting from the start of a new sequence of integer-bearing records.
As soon as a non-numeric is sensed in the first character, reset ICOUNT to zero and loop back to reading records
0 Kudos
Jugoslav_Dujic
Valued Contributor II
938 Views
If I recall correctly (although I admit I mix it up often, so take it with a grain of salt), you can't use list-directed format (*) to read the entire line at once -- the read will stop on first blank. You need either "(A)" or "(A100)" (100 being len(string)) instead.
0 Kudos
Reply