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 a .txt file

Status
Not open for further replies.

ARR86

Programmer
Apr 6, 2012
5
DE
I am a novice in fortran programming.I want to read text files containing three columns of data ( one integer and 2 real. Each file contain data of different lengths.Could someone please help me by providing the code?.

Below is the data from one of the files to be read.

1 0.0000000E+00 0.2000000E+00
2 0.1500000E-01 0.2000000E+00
3 0.3000000E-01 0.2000000E+00
4 0.4500000E-01 0.2000000E+00
5 0.6000000E-01 0.2000000E+00
6 0.7500000E-01 0.2000000E+00
7 0.9000000E-01 0.2000000E+00
8 0.1050000E+00 0.2000000E+00
9 0.1200000E+00 0.2000000E+00
10 0.1350000E+00 0.2000000E+00
11 0.1500000E+00 0.2000000E+00
12 0.1650000E+00 0.2000000E+00
13 0.1800000E+00 0.2000000E+00
14 0.1950000E+00 0.2000000E+00
15 0.2100000E+00 0.2000000E+00
16 0.2250000E+00 0.2000000E+00
17 0.2400000E+00 0.2000000E+00
18 0.2550000E+00 0.2000000E+00
19 0.2700000E+00 0.2000000E+00
20 0.2850000E+00 0.2000000E+00
21 0.3000000E+00 0.2000000E+00
22 0.3150000E+00 0.2000000E+00
23 0.3300000E+00 0.2000000E+00
24 0.3450000E+00 0.2000000E+00
25 0.3600000E+00 0.2000000E+00
26 0.3750000E+00 0.2000000E+00
27 0.3900000E+00 0.2000000E+00
28 0.4050000E+00 0.2000000E+00
29 0.4200000E+00 0.2000000E+00
30 0.4350000E+00 0.2000000E+00
31 0.4500000E+00 0.2000000E+00
32 0.4650000E+00 0.2000000E+00
33 0.4800000E+00 0.2000000E+00
34 0.4950000E+00 0.2000000E+00
35 0.5100000E+00 0.2000000E+00
36 0.5250000E+00 0.2000000E+00
37 0.5400000E+00 0.2000000E+00
38 0.5550000E+00 0.2000000E+00
39 0.5700000E+00 0.2000000E+00
40 0.5850000E+00 0.2000000E+00
41 0.6000000E+00 0.2000000E+00

Regards
Andy.
 
... and what would be your idea ?

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
I have written a program but it reads only the first line. I dont know how to make it return again and read the next line.

Here it is

PROGRAM nodes1
Integer K,N
double precision XPOS,RAD
OPEN(25,file='nodes_cylinder.txt')
WHILE(K.NE.-1) DO
READ(25,100,IOSTAT=K)N,XPOS,RAD
c 7 FORMAT(I2,2(2X,E8.1))
100 FORMAT(I2,2(2X,E12.15))
END DO
CLOSE(25)
WRITE(25,300)N
300 FORMAT(1X ,'N=',I2)
WRITE(*,*)K

END PROGRAM
 
You could try this
Code:
PROGRAM nodes1
    Integer N
    double precision XPOS,RAD
    OPEN(25,file='nodes_cylinder.txt')
    DO WHILE(.true.)
        READ(25,100,END=200)N,XPOS,RAD
100     FORMAT(I2,2(2X,E12.15))
    END DO
200 CLOSE(25)  
    WRITE(25,300)N
300 FORMAT(1X ,'N=',I2)
END PROGRAM
 
Most possibly, to do something useful later on, you may have to turn your variables XPOS and RAD into arrays.

If you do no know ahead of time how many lines the input file is going to have, you may want to read it once just to count the number of lines and then a second time for real. The first time, do not read 'integer, real, real', simple read 'integer' and ignore the rest...after all, you 'have not' allocated the array, yet.

Then, after you know how long the file is and you have N, you can then allocate the arrays, close the file, re-open it, read.
 
I'd say, your program needs some further improvements to run successfully:

100 FORMAT(I2,2(2X,E12.15))

In the E-format descriptor, as in all other formats, the second number - the one behind the dot - cannot be bigger than the first. Maybe your compiler is friendly and ignores the 15 and the read executes properly, but still this is not good programming.

CLOSE(25)
WRITE(25,300)N
300 FORMAT(1X ,'N=',I2)

Close IO-channel 25 and then write to it ? Hmmm...

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Hi all

I compiled the program above and the format(I2,2(2X,E12.15)) did not work properly on the data in the file. So I changed the format somewhat and added a write to an output file to check this. Now the input seems to work for N >= 100 also.

Code:
	PROGRAM nodes1

	Integer N(200),i,j,No
	double precision XPOS(200),RAD(200)

	OPEN(25,file='nodes_cylinder.txt')
	j = 0
	DO WHILE(.true.)
	   j = j+1
	   READ(25,100,END=200) N(j),XPOS(j),RAD(j)
	   write(*,*) N(j),XPOS(j),RAD(j)
100	   FORMAT(I3,2E15.7)
	END DO
200	continue
	No = j-1
	CLOSE(25)  

	WRITE(*,300) No
300	FORMAT(1X ,'No = ',I3)

	open(25,file='nodesout.txt')
	do i = 1,No
	   write(25,150) N(i),XPOS(i),RAD(i)
150	   FORMAT(I3,2E15.7)
	enddo
	close(25)

	END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top