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

Reading vectors from file

Status
Not open for further replies.

ferry2

Technical User
May 6, 2011
20
RO
I write a program for solving tridiagonal linear system of equations and I want to modify it - to read input data from file. The content of the file is:

n u v w r

4 0 3 2 0.5
2 3 1 4.2
-2 5 -3 7
1 4 0 9
where n is the dimension of the system and the u, v, w, r are the input vectors. How to read first the "n" variable then each vector one by one?
 
Something like :

Code:
read(*,*) n,(u(i),v(i),w(i),r(i),i=1,n)

François Jacq
 
I'm getting Unhandled Exception 0xC000008C: Array Bounds Exceeded.
 
Because, probably, the arrays u,v,w,r are too small. Their size must be greater than or equal to n.

If you have no idea about the value of n, you could try something like :

Code:
real,allocatable :: u(:),v(:),w(:),r(:)
integer :: n,i
open(15,file="my_data_file")
read(15,*,advance='no') n
allocate(u(n),v(n),w(n),r(n))
read(15,*) (u(i),v(i),w(i),r(i),i=1,n)



François Jacq
 
Francois,
I bet there will be an error with this code again for there seems to be a different format of line 1 to the rest. So I would advise:

Code:
character*100 cBuffer
real, allocatable :: u(:), v(:), w(:), r(:)
integer n, i, iDummy
open (unit = 15, file = 'my_data_file'
read (15, '(a100)') cBuffer  ! read the first line into a buffer
read (cBuffer,*) n           ! read the first integer from buffer
allocate (u(n), v(n), w(n), r (n))
read (cBuffer, *) iDummy, u(1), v(1), w(1), r(1) ! read the valid data from the first line
read (15,*) (u(i), v(i), w(i), r(i), i = 2, n) ! read n-1 lines

The crucial thing here seems to be to read one integer and four reals from the first line and only four reals from the following lines. If you do this by buffer or a no_advance-read is of no real importance.

Norbert
 
No.

There is no mistake in my solution because I precised advance="no" in the first read statement.

But your solution should work too even if it is a little bit longer than mine.

François Jacq
 
Yes, I see that you read the first line a second time. But you only read four reals from it whereas there is one integer and four reals in this line, five figures in total.
Your code would read the first line as 4.0, 0.0, 3.0, 2.0 to u, v, w, r
whereas mine would read 0.0, 3.0, 2.0, 0.5 to the same.

Norbert
 
No again : you don't understand advance="no".

My first READ statement reads only the value of "n" in setting the file position just after that value. Without advance="no", the remaining part of the first line would be lost because the file position would be set at the beginning of the second line.

After that, my second read statement works correctly because the value of "n" is already skipped : it remains only 4*n real values to read from that file

François Jacq
 
Oh, thanks. I was not aware of this. I thought advance=no would just keep the record, not the position in the record.

Something learned tody. Quite a good start.

Norbert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top