Click here to Skip to main content
15,847,750 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to read variables from a file in Fortran. For example I have one .txt file with multiple columns. I would like to set i.e.
x (for the first column)
y (for the secοnd column)
z (for the third column) etc.

The porpuse of this set is to create a code which would use one equation and after that it would makes claculation between x,y,z etc.

Could you please tell me which command should I use in order to make it?

I have to mention that every time I would not have the same dimension (number of lines and rows) files.

What I have tried:

The problem is that I have a file with 14 columns and 644 lines. I use these commands but my .exe file does not showing what I would like


I give you my code:
FORTRAN
parameter (len=644)          ! lines
	dimension A(len,14)           !rows
C       real  IMM, lat, lon         	character header*110,Var1(len)*4,Var2(len)*8,Var3(len)*8
     +	,Var4(len)*8,Var5(len)*8,Var6(len)*8,Var7(len)*8,Var8(len)*8,Var9(len)*8,
     +  ,Var10(len)*8, Var11(len)*8,Var12(len)*8,Var13(len)*8,Var14(len)*8,header_append*97
	open(7,file='testdata.txt',status='old')
	open(8,file='EX-1.out',status='unknown')

        do i=1,len
        write(*,5) A(i,1)
        end do


5       format(2x,f3.1)

        do i=1,ip
        write(*,5) A(i,1)
        end do

        stop
        end

Could you please help me?
Posted
Updated 4-Jan-22 3:45am
v5
Comments
Patrice T 4-Jan-22 3:38am    
"but I am not sure hoe to syntax it"
too bad there is no documentation.
Richard MacCutchan 4-Jan-22 8:46am    
"file does not showing what I would like"
Sorry, no one here can guess what that means. Please explain exactly what results you are trying to get and why they are not correct.
Member 14991075 4-Jan-22 8:49am    
I would like to check if the commands of my code read my data of my input file. But this code show one 1x1 column with zeros (0) and do not show the data of the first column of my input file
Richard MacCutchan 4-Jan-22 8:53am    
You never read from any file.
Member 14991075 4-Jan-22 8:58am    
I use command
open(7,file='testdata.txt',status='old')

It is no use?
testdata.txt is my input file

As @Patrice-T has suggested - read the documentation READ (FORTRAN 77 Language Reference)[^]
Or one of the on-line tutorials - Fortran 77 Tutorial[^]

And here is an example of using a loop to read the file Read Entire line regardless of content/delimiters - Fortran - Tek-Tips[^]
 
Share this answer
 
You were given an answer four days ago in your original question at How do I read variables from a txt file in Fortran[^]. Please do not repost.
 
Share this answer
 
Comments
Member 14991075 4-Jan-22 8:09am    
The problem is that I have a file with 14 columns and 644 lines. I use these commands but my .exe file does not showing what I would like


I give you my code:
parameter (len=644) ! lines
dimension A(len,14) !rows
C real IMM, lat, lon
character header*110,Var1(len)*4,Var2(len)*8,Var3(len)*8
+ ,Var4(len)*8,Var5(len)*8,Var6(len)*8,Var7(len)*8,Var8(len)*8,Var9(len)*8,
+ ,Var10(len)*8, Var11(len)*8,Var12(len)*8,Var13(len)*8,Var14(len)*8,header_append*97
open(7,file='testdata.txt',status='old')
open(8,file='EX-1.out',status='unknown')

do i=1,len
write(*,5) A(i,1)
end do


5 format(2x,f3.1)

do i=1,ip
write(*,5) A(i,1)
end do

stop
end

Could you please help me?
Richard MacCutchan 4-Jan-22 8:48am    
I have moved this information to the question. See my comment below it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900