Fortran: read and write data file


haris3384
08-07-2012, 08:02 AM
hi

i am new to this forum and in using fortran, i have an input file which looks like

1 e:1 s:5 n= 128 5 1 52

4 e:10 s:6 n= 38 20 117 132

7 e:141 s:3 n= 1 301 25 3

............... ............... ...............

147 e:19 s:3 n= 127 132 131 126

what i want is to read this input file and write output file in the following format


1 P5 128 5 1 52

10 P6 38 20 117 132

141 P3 1 301 25 3

............... ...............

19 P3 127 132 131 126

someone please guide me regarding this

davekw7x
08-08-2012, 03:58 PM
...someone please...
Well, meaningful guidance depends on context.

If this is a Fortran class and you are supposed to be learning about integers and strings and character arrays and files and stuff like that, then I recommend that you refer to textbook or class notes or whatever other material you are learning from.

If you try some example and don't understand how or why it works or doesn't work, post some code and ask specific questions.

On the other hand, if this is some kind of applications course (lab or whatever) and all you want to do is get the job done, then I think it might be appropriate to use Dr. George Benthien's excellent "strings" module that you can download from his web site: Fortran String Utilities (http://gbenthien.net/strings/index.html). (That's what I would do.) There is documentation with the downloaded code.

Here's a test program that reads the lines that you said represented your data:

!
! Subroutines "parse" and "value" are in the "strings"
! module, which comes from stringmod.f90.
!
! Here's where you can get stringmod.f90 (other stuff, too)
!
! http://www.gbenthien.net/strings/index.html
!
! This example from davekw7x
!
! Input file has lines like this
! 1 e:1 s:5 n= 128 5 1 52
! 4 e:10 s:6 n= 38 20 117 132
! 7 e:141 s:3 n= 1 301 25 3
! 147 e:19 s:3 n= 127 132 131 126
!
!
Program ParserDemo
USE strings

IMPLICIT NONE

! The line counter
Integer :: i
Integer :: j !Used in debugging loop

! Each line is read into a string
Character (len=80) :: line

! An array of strings to hold the individual fields on the line
Character (len=80) :: tokens(80)

! The number of fields on the line
Integer :: ntokens

! The error flag from the parse subroutine
Integer :: ios

! What we are looking for
Integer :: first, second, third, fourth, fifth, sixth

open(7,file='data.txt',status='old')

do i=1,100
read(7,'(A)',END=1000,ERR=999) line

!Delimiters are colon and space
call parse(line, ': ', tokens, ntokens)

if (ntokens > 0) then
!Uncomment the following four statements for debugging
!write(*,'(" Line ",I0)') i
!do j=1,ntokens
!write(*,'(" tokens(",I0,"): <",A,">")') j, tokens(j)
!enddo

call value(tokens(1), first, ios);
call value(tokens(3), second, ios);
call value(tokens(5), third, ios);
call value(tokens(8), fourth, ios);
call value(tokens(9), fifth, ios);
call value(tokens(10), sixth, ios);
write (*,'("Line ", i0," values =", 6(i5,1x))') &
i, first, second, third, fourth, fifth, sixth
endif ! end of if(ntokens > 0
enddo
999 write(*,'(" Error reading line ", I0)') i
close(7)
stop

1000 write(*, '(" Number of lines read = ", I0)') i-1
close(7)
stop

END Program ParserDemo


Output:

Line 1 values = 1 1 5 5 1 52
Line 2 values = 4 10 6 20 117 132
Line 3 values = 7 141 3 301 25 3
Line 4 values = 147 19 3 132 131 126
Number of lines read = 4


Note that you should check the the number of tokens after reading a line and check the value of ios after each attempt to get a value from the line's tokens. This is important, but I thought I would leave it up to you to Do The Right Thing to make sure your data is what it should be. (For this test program I printed out all of the values, so, I didn't really feel the need to do a separate check of ios each time.)



Regards,

Dave

haris3384
09-08-2012, 04:07 AM
hi

firstly many thankx for your help... the program worked for me... i also need your kind guidance for another problem

i have one file (file-1) containing following data

1 2.01E5
2 2.15E5
3 2.18E5
4 2.06E5
5 2.21E5
6 2.35E5

and other file (file-2) containing data

8 2 1 2 3 4
9 2 1 3 5 6
10 3 2 3 4 6
11 5 1 2 4 5

using data from these two files i want to write third file containing

8,P2,AVG(1,2,3,4)
9,P2,AVG(1,3,5,6)
10,P3,AVG(2,3,4,6)
11,P5,AVG(1,2,4,5)

where AVG(1,2,3,4) is the ((2.01E5+2.15E5+2.18E5+2.06E5)/4)

kindly guide me how to proceede