|
Prev: open file in subroutine
Next: Linking issue
From: elijah on 23 Apr 2008 06:08 program main integer ln real newval,oldval ln=178 open(unit=10,file='file.txt', & status='old',form='FORMATTED',access='sequential') rewind 10 do n=1,ln-1,1 if(n.eq.ln)then read(10,20)oldval endif enddo write(*,*)'Old value was' write(*,20)oldval do n=1,ln-1,1 if(n.eq.ln)then write(10,20),newval endif enddo write(*,*)'New value is' write(*,20)newval 20 format(10x,f10.3) close(10) end program in the above program I would like to modify the columns 11-20 in line 178 of "file.txt". Text file contains lines of different lengths, character strings, floating point numbers etc. It seems that I am unable to locate porperly the columns and/or the line. Any help will be much appreciated. regards MI
From: e p chandler on 23 Apr 2008 07:22 On Apr 23, 6:08 am, elijah <mily...(a)gmail.com> wrote: > program main > integer ln > real newval,oldval > ln=178 > open(unit=10,file='file.txt', > & status='old',form='FORMATTED',access='sequential') > rewind 10 > do n=1,ln-1,1 > if(n.eq.ln)then > read(10,20)oldval > endif > enddo > write(*,*)'Old value was' > write(*,20)oldval > do n=1,ln-1,1 > if(n.eq.ln)then > write(10,20),newval > endif > enddo > write(*,*)'New value is' > write(*,20)newval > 20 format(10x,f10.3) > close(10) > end program You can not both read and write a sequential file at the same time. You can read ... modify ... write a direct access file, but this type of file access requires a fixed record length. > in the above program I would like to modify the columns 11-20 in line > 178 of "file.txt". Text file contains lines of different lengths, > character strings, floating point numbers etc. 1. The usual way to handle this is to use two files. Copy lines as is from old file to new file. Read the target line, modify it, write it to the new file. Copy the rest of the source file to the destination file. Delete the old source file. Rename the destination file. 2. Fortran is not the way I prefer to handle these text modification tasks. If you only have one line of data to modify, why not use a text editor and do it manually? Otherwise any one of a number of scripting languages would be better choices for this task. 3. Without a better idea of what defines a "column", it is difficult to solve your task. > It seems that I am unable to locate porperly the columns and/or the > line. > Any help will be much appreciated. 4. Search this newsgroup for other postings on this topic. IMO "How do I modify a data file, in place?" has joined the ranks of the FAQ in this newsgroup. -- e
From: elijah on 23 Apr 2008 07:47 > 3. Without a better idea of what defines a "column", it is difficult > to solve your task. $# nrcyck drtol drfctr drterm tssfdr irelal edttl idrflg 250 0.001000 0.995000 0.000 0.000 0 0.040000 *CONTROL_ENERGY $# hgen rwen slnten rylen 2 2 2 2 this is an excerpt from the file. above is the longest line in the text file. lines do not have one fixed length. I am considering each character "space" as one column.
From: Arjen Markus on 23 Apr 2008 07:54 On 23 apr, 12:08, elijah <mily...(a)gmail.com> wrote: > program main > integer ln > real newval,oldval > ln=178 > open(unit=10,file='file.txt', > & status='old',form='FORMATTED',access='sequential') > rewind 10 > do n=1,ln-1,1 > if(n.eq.ln)then > read(10,20)oldval > endif > enddo > write(*,*)'Old value was' > write(*,20)oldval > do n=1,ln-1,1 > if(n.eq.ln)then > write(10,20),newval > endif > enddo > write(*,*)'New value is' > write(*,20)newval > 20 format(10x,f10.3) > close(10) > end program > > in the above program I would like to modify the columns 11-20 in line > 178 of "file.txt". Text file contains lines of different lengths, > character strings, floating point numbers etc. > > It seems that I am unable to locate porperly the columns and/or the > line. > Any help will be much appreciated. > > regards > MI Well, if you use the READ statement like this, it will simply read a part of the line and then jump to the next line. I'd say: character(len=100) :: line ! Or whatever the length is ! the longest line do read( 10, '(a)' ) line line(11:20) = ... new value ... write( 20, '(a)' ) line enddo (with all manner of details left out) Regards, Arjen
From: e p chandler on 23 Apr 2008 08:10
On Apr 23, 7:47 am, elijah <mily...(a)gmail.com> wrote: > > 3. Without a better idea of what defines a "column", it is difficult > > to solve your task. > > $# nrcyck drtol drfctr drterm tssfdr irelal > edttl idrflg > 250 0.001000 0.995000 0.000 0.000 0 0.040000 > *CONTROL_ENERGY > $# hgen rwen slnten rylen > 2 2 2 2 > > this is an excerpt from the file. above is the longest line in the > text file. lines do not have one fixed length. I am considering each > character "space" as one column. So you want to replace the contents in print positions 11 to 20 with something else? Again, you want to over-write the old data with the new, leaving the length of the original line unchanged? - e |