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

Read a txt file

sivatt
Beginner
723 Views
Hi,
I'm trying to read a .txt file containing a 247 by 200 matrix. These are commands I used:
open(6, file = 'yfortran.txt', status = 'old', form = 'formatted', action = 'read')
do i = 1, 247
read(6, *) (y(i, j), j = 1, 200)
end do
close(6)
I kept getting error msg: ...has triggered a breakpoint
Could anyone give me some suggestions? One more question: how can I read an array with unknown size?
Thanks,
Siva
0 Kudos
7 Replies
Steven_L_Intel1
Employee
723 Views
The real error message is in the console window which you are not seeing. Look in the Windows taskbar tray for the icon for the console window and click on it to bring it to the top.

You can't read an array with unknown size - you have to figure out what its size is first. You can read the size from the file and use an ALLOCATABLE array which allows you to ALLOCATE it to the desired size as the program is run.

You may also be able to use END= or IOSTAT= to detect when you have reached the end of the file. I don't know your application so can't say which approach is best.
0 Kudos
sivatt
Beginner
723 Views
Thanks a lot. The console window shows:
forrtl: severe (408): fort: (10): subscript #2 of the array Y has value 1 which is greater than the upper bound of -1.
I guess this is because the file I am trying to load is atab-delimited text file, not really an array... What shall I do if I want to read it as a matrix?
Thanks
0 Kudos
sivatt
Beginner
723 Views
Hi Steve,
I modified my code to
integer, parameter :: p = 15372, n = 247, s = 200
real, dimension(:,:), allocatable :: x, y
integer :: i, j
allocate (estb(p, s), x(n, p), y(n, s))
open(5, file = 'xfortran.txt', status = 'old', form = 'formatted', action = 'read')
open(6, file = 'yfortran.txt', status = 'old', form = 'formatted', action = 'read')
do i = 1, n
read(5, *) (x(i, j), j = 1, p)
read(6, *) (y(i, j), j = 1, s)
end do
close(5)
close(6)
now the error is:
input conversion error, unit 5...
Any suggestions?
0 Kudos
Steven_L_Intel1
Employee
723 Views
Input conversion error means that the data read isn't valid for numeric list-directed input. Without also seeing the data files, I can't comment more specifically.
0 Kudos
sivatt
Beginner
723 Views
Thanks! You are right, the first row of the data contains strings of column names. How can I read the data from the second row (the numbers)?
0 Kudos
anthonyrichards
New Contributor III
723 Views
add a READ statement with the correct A format to read the first record into a character string of length long enough to contain the whole record. This will position the file ready for you to start your read of the numbers.
0 Kudos
sivatt
Beginner
723 Views
Thanks Anthony. I modified the statement and now it reads fine.
0 Kudos
Reply