dealove
07-08-2008, 01:53 PM
I have a file with text and adjacent to it, the number i want to read. Below is the format. I was wondering how to read just the numbers without touching the text.
trhy 30.0
difje 30.0
sdjk 66.5
xmb 15.0
dcos 15.0
fjvm 2.573e-4
srht 9.05e-3
art3 0.0
19.954
39.499
43.852
48.713
51.660
53.801
54.715
55.602
another format is:
is...o2. mw2 31.998 htf2 0.0
is...n2. mw3 28.014 htf3 0.0
is..co2. mw4 44.009 htf4 -93.965
is..h2o. mw5 18.015 htf5 -57.103
if above two formats are in the same file, can I use DO loops for first format and second format individually by counting the number of lines?
davekw7x
07-09-2008, 12:36 AM
I have a file.
Before you write a program to read stuff from a file, I respectfully suggest that you ask your self the following two questions (and, of course you answer them yourself, since no one can answer them for you):
1. What, exactly is the format of the file? What is where? How many of them are there.
2. What, exactly are you going to do with the stuff that you read?
Like the soup kitchen: First the sermon, then the meal.
The sermon has two parts: Plan A and Plan B.
Plan A:
If you are using Fortran as a problem solving tool (reading in some numbers and doing some calculations and analysis) then you should realize that Fortran is best (easiest) when it is presented with data in a regular format. I mean, it's possible to write Fortran that reads an arbitrarily jumbled mix of numeric and alphabetic data items and throw away everything that's not numeric and do something specific with the numbers, but is that how you really design a project?
Plan B:
On the other hand, if you are studying to become an expert Fortran programmer so that you can create code for others to use on some wildly irregularly formatted file, then you can read each line into a character array and parse it character by character to decide when a non-numeric data item starts and ends and when numeric data in fixed and/or floating point format is encountered and convert to the numeric value (and save it---where?)
I'll go for Plan A.
I pasted stuff from your post into the following file, and saved it as "indata.txt":
trhy 30.0
difje 30.0
sdjk 66.5
xmb 15.0
dcos 15.0
fjvm 2.573e-4
srht 9.05e-3
art3 0.0
19.954
39.499
43.852
48.713
51.660
53.801
54.715
55.602
#######################
is...o2. mw2 31.998 htf2 0.0
is...n2. mw3 28.014 htf3 0.0
is..co2. mw4 44.009 htf4 -93.965
is..h2o. mw5 18.015 htf5 -57.103
I wrote a program according to the following specification, which I made to be consistent with what I see in this example data.
1. Each of the first sixteen lines consists of "something" to be ignored in columns 1-8, followed by a floating point number. The number can be in fixed decimal or exponential floating point decimal notation.
2. There is a line with "a bunch" of characters; the maximum number of characters on the line is 50
3. There are an unknown number of lines following that. Each line has 15 columns of "something" that is to be ignored. Then there is a fixed point decimal number in 10.3 format. Then there are seven columns of "something" that is to be ignored. Then there is a floating point number that can be in fixed decimal or exponential floating point decimal notation.
The assignment is:
1. Print the line number and numeric value of the number on lines 1-16.
2. Print line 17, verbatim.
3. Print the line number and numeric values of the numbers from the remaining lines.
All numbers from the files are to be printed in Engineering notation with 7 digits after the decimal point.
Finally, print a message telling the total number of lines in the file.
Here's my code (compiles with GNU g77 or gfortran; should be OK with any recent Fortran compiler):
PROGRAM testread
implicit none
integer line
character*50 cline
double precision x, y
open (unit=1,file="indata.txt",status="old")
do line = 1,16
read(1,'(8x,e20.7)', END=1000,ERR=1000) x
write (*,'(I5, ": x = ", 1pe14.7)') line, x
enddo
read(1,'(A)') cline
line = 17
write(*,*) cline
do line = 18,100
read(1,'(15x,f10.3,7x,e20.7)', END=1000,ERR=1000) x, y
write (*,'(I5, ": x = ", 1pe14.7, ", y = ",1pe14.7)') line,x,y
enddo
1000 close(1)
write(*,'("Number of lines = ", I5)') line-1
END PROGRAM testread
Here's the output:
1: x = 3.0000000E+01
2: x = 3.0000000E+01
3: x = 6.6500000E+01
4: x = 1.5000000E+01
5: x = 1.5000000E+01
6: x = 2.5730000E-04
7: x = 9.0500000E-03
8: x = 0.0000000E+00
9: x = 1.9954000E+01
10: x = 3.9499000E+01
11: x = 4.3852000E+01
12: x = 4.8713000E+01
13: x = 5.1660000E+01
14: x = 5.3801000E+01
15: x = 5.4715000E+01
16: x = 5.5602000E+01
#######################
18: x = 3.1998000E+01, y = 0.0000000E+00
19: x = 2.8014000E+01, y = 0.0000000E+00
20: x = 4.4009000E+01, y = -9.3965000E+01
21: x = 1.8015000E+01, y = -5.7103000E+01
Regards,
Dave