Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to read data from a formatted text file 1

Status
Not open for further replies.

matthewkmk

Programmer
Nov 18, 2002
19
MO
If I have a file in this format:
Actually this is a long file, I just want to get some data from it.

...........
*
*******************
* Class 1
******************
Name No
John 1
Mary 2
..............

******************
* Detail
******************
Name Age
John 13
Mary 15
................

I want to get the information about John and Mary.
If I open the text file, how to get the data once the computer finds the words *Class 1 and then after some
row, I also want to get another data below *Detail

Thx a lot.
 
as promised.. here's code and output,
developed with SALFORD pers fortran:
character*10 name(20),temp
character*80 line
character*1 gender(20),class(20)
character*3 age(20)
integer*2 which,i,ic,arrlen,arrloc

arrlen = 0
arrloc = 0
which = 0
i = 0
ic = 0

open(unit=6,file='t.txt')

100 format(a80)
110 format(1x,i2,1x,a10,1x,a3,1x,a3,1x,a2)
10 read(6,100,end = 20) line

* Get rid of comments in the data

if (line(1:1).eq.'*') then
go to 10
endif

* Get the heading for the category
* and decide what array to use
* Since this is a heading, assume fixed format

if (line(1:4) .eq. 'Name') then
which = 0
if (line(7:8) .eq.'No') then
which = 1
endif
if (line(7:9) .eq.'Age') then
which = 2
endif
if (line(7:12) .eq.'Gender') then
which = 3
endif
goto 10
endif

* This must be the data to capture
* parse out the name - find the first blank

ic = index(line,' ')
temp = line(1:ic-1)


* if it's a name add it to the array or know we have it

if (arrlen .eq.0) then
arrloc = 1
arrlen = 1
name(1) = temp
goto 40
else
do 30 i = 1,arrlen
if (name(i).eq. temp) then
arrloc = i
goto 40
endif
30 continue
arrlen = arrlen + 1
arrloc = arrlen
name(arrlen) = temp
endif

* Kill the spaces between name and data

40 line = line(5:len(line)-ic)
50 line = line(2:len(line)-1)
if (line(1:1) .eq. ' ') then
goto 50
endif

* Put the data in the appropriate aray

if (which.eq.1) then
class(arrloc) = line(1:leng(line))
endif
if (which.eq.2) then
age(arrloc) = line(1:leng(line))
endif
if (which.eq.3) then
gender(arrloc) = line(1:1)
endif

goto 10
20 continue
close(5)
do 300 i = 1,arrlen
300 print 110,i,name(i),class(i),age(i),gender(i)

end


OUTPUT:
1 John 1 13 M
2 Mary 2 15 F
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top