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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i read and re-write several specific lines?

Status
Not open for further replies.

nmsc

Programmer
Nov 5, 2009
2
BR
I'm working on a project so i would be glad if someone answer as soon as possible. The thing is that i need to read some lines and re-write only a specific column, i already get the 'unique' column but on it appears just one line... the first

Here it's...
----------------------------
program

implicit none
real Charge, coord_x, coord_y, coord_z, mass
integer Atom, Z, flag, prim, iend
character c_sn*1

open (File='enter.txt',Unit=40,Status='OLD',&
form='FORMATTED', access='direct', recl=150)

prim = 001
flag=.false.

rewind 40
do
read(40,20,iostat=iend) Atom, Z, c_sn, Charge, c_sn,&
coord_x, c_sn, coord_y, c_sn, coord_z, mass

20 format (i3, 2x, i2, 4x,&
a1, f8.6, 6x, a1, f7.5, 4x,&
a1, f7.5, 4x, a1, f7.5, 6x, f8.5)



if(Atom.eq.prim)then
flag =.true.
exit

endif
if(iend.lt.0)exit

if(flag)then

read(40,20) Atom, Z, c_sn, Charge, c_sn,&
coord_x, c_sn, coord_y, c_sn, coord_z, mass

endif
enddo

write(*,*)' ' !blank line


write(50,10) c_sn, Charge

10 format('Charge',//,a1,f8.6)

close (50)
close (40)

stop
end
----------------------------

i need to read a archive like this...

NET CHARGES AND COORDINATES
Atom Z Charge Coordinates(Angstrom) Mass
x y z
1 6 -0.115485 0.00006 -0.00008 -0.00001 12.01100
2 6 -0.033024 1.38997 -0.00012 0.00000 12.01100
3 6 -0.449862 2.11881 1.18739 0.00001 12.01100
4 6 -0.050770 1.42026 2.39266 0.00824 12.01100


but only the third column. So please tell what am i doing wrong?
 
I'm sorry, but I don't understand your code really well.

Is the input file binary or ascii??

With binaries I don't have much experience, with ascii just..

Code:
REAL, DIMENSION(3) :: var

OPEN(UNIT=1,FILE='enter.txt',STATUS='OLD')
OPEN(UNIT=2,FILE='thirdcolumnonly.txt',STATUS='REPLACE')
READ(1,*)
READ(1,*)
DO WHILE(.NOT.EOF(1))
   READ(1,*)(var(j),j=1,3,1)
   WRITE(2,*)var(3)
END DO
CLOSE(2,STATUS='KEEP')
CLOSE(1,STATUS='KEEP')

Possibly too simplistic, I guess your problem is different...
 
I'm guessing similar things

1) Do you need direct access
2) Is your record length really 150 - do you have blank spaces until the 150th character. Does this include CR/LF

If you set the recl to 150 and the line length is only 50, then it will read 3 lines in one go.
 
i have a doubt... the record lenght is for the max lenght that exists on data or the lenght of line that i need?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top