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 table input from a .txt file 1

Status
Not open for further replies.

bobbydrake

Technical User
Jun 12, 2012
2
ID
dear all,
i have this problem inputing data. i have this following data:
index i j value
1 1 2 2
2 1 3 10
3 2 3 10
4 2 4 5
5 3 4 6
i need this data to read it in fortran and make it as a matrix a(i,j).
'i' mean the row of a matrix a, and 'j' column of matrix a.
example, from index 1, it has to be a(1,2)=2
from the given data it will produces matrix:
a=
0 2 10 0
0 0 10 5
0 0 0 6
0 0 0 0

would you help me through this? :) many thanks before.
 
Please, show us your program, especially how you read your array and how you print it.

François Jacq
 
Try something like this

Code:
program read_table                                                                        
  implicit none                                                                           
  integer, parameter :: size_matrix = 4                                                   
  integer            :: stat                                                              
  integer            :: i, j, ind, val                                                    
  integer            :: matrix(size_matrix, size_matrix)                                  
                                                                                          
  open(unit=100, file='input.dat')                                                        
                                                                                          
  matrix = 0                                                                              
  do                                                                                      
    read(100,*,iostat=stat) ind, i, j, val                                                
    if (stat .ne. 0) exit                                                                 
    matrix(i,j) = val                                                                     
  enddo                                                                                   
                                                                                          
  do i = 1, size_matrix                                                                   
    print *, matrix(i,:)                                                                                                                                                
  enddo                                                                                   
                                                                                          
  close(100)                                                                              
                                                                                          
end program read_table
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top