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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read every single character of file into array

Status
Not open for further replies.

choclo

Technical User
Dec 29, 2007
3
GB
My problem is as following: I have an unformatted text file of an unknown number of lines and length of a line.
I would like to read every single character into an array char(i,j) where i is the line-number and j is the character position of that line.
So far I have only achieved to read the first letter of each word.

Thanks for your help.
 
What do your open and read statements look like?
 
Right! Here the code I have been working on. The problem is that it stops reading in the single characters of a line at occurrence of a blank although it is supposed to stop reading at the line break.

Code:
	program read_in

	implicit real*8 (a-h,o-z)

	character (Len=1),dimension(:,:),allocatable :: abc

	integer, parameter :: linemax=500
	character*130 line

	NELEMX=500
	NELEMY=500

	allocate(abc(NELEMX,NELEMY))
	

! read in line
	open(1,file='test.txt',status='old')
		k=0
		do
			read(1,'(A)',end=999) line
			k=k+1
! read value from the line
			read(line,*,end=888) (abc(k,i),i=1,linemax)
			888 continue
		enddo
999 continue
	close (1)

do j=1,5
print *, abc(2,j)
enddo



! clean up
	deallocate(abc)

	return
	end
 
I mean *at occurrence of a space*
 
Change
Code:
            read(line,*,end=888) (abc(k,i),i=1,linemax)
            888 continue
to
Code:
            linelen = len(trim(line))
            do i = 1, linelen
               abc(k,i) = line(i:i)
            enddo
If you specify * in a read, it leaves the runtime to figure out what it is. It may make the wrong decision and do something completely unexpected, especially with characters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top