For Programmers: Free Programming Magazines  


Home > Archive > Fortran > April 2007 > Fortran to edit one line of text file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Fortran to edit one line of text file
John S

2007-04-18, 7:05 pm

Due to historical reasons, have a long formated sequential (text) file of
data. Need to re-write one of the early header cards. Tried the open and
rewrite scheme below, but rest of file disappears after rewriting the
required line.
So, short of copying the rest of the file, how can one do this?

File needs to be sequential & formated:

The open is

open( iU, file = cFileName )
write(iU,'(a)') ' VERSION_1 '

write(iU,'(a)') ' some text'

write(iU,'(79a)') cTitle


write(iU,*) rReal, iDum1, iDum2, iDum3

write 1000's of lines of data.



Now, need to change line 4 and keep all data following line 4. The
following deletes all data after line 4.

rewind( iU )


do i = 1, 3

read(iU,*)

enddo


read(iU,*) rReal, iDum1, iDum2, iDum3

backspace(iU)


iDum3 = iNewValue ! change parameter value


write(iU,*) rReal, iDum1, iDum2, iDum3

close(iU)

So, how to change only one line of this data file without reading /
rewriting the entire file.

Thanks.


J. F. Cornwall

2007-04-18, 7:05 pm

John S wrote:
> Due to historical reasons, have a long formated sequential (text) file of
> data. Need to re-write one of the early header cards. Tried the open and
> rewrite scheme below, but rest of file disappears after rewriting the
> required line.
> So, short of copying the rest of the file, how can one do this?
>



(snip)

>
> So, how to change only one line of this data file without reading /
> rewriting the entire file.
>
> Thanks.


If you want to do this in Fortran, you'll have to read and rewrite the
rest of the file. Or, you could use one of the multitude of scripting
languages such as perl, Ruby, python, etc, or the old standby's awk or
sed to do this editing.

Jim

jamesgiles@att.net

2007-04-18, 7:05 pm

On Apr 18, 9:00 am, "J. F. Cornwall" <JCornw...@cox.net> wrote:
> John S wrote:
> [...] Tried the open and
....[color=darkred]
> If you want to do this in Fortran, you'll have to read and rewrite the
> rest of the file. Or, you could use one of the multitude of scripting
> languages such as perl, Ruby, python, etc, or the old standby's awk or
> sed to do this editing.


Of course most of those will also read and rewrite the whole
file as well. Certainly sed (Stream EDitor) will. I note that the
original code uses list-directed writes. There's no reason to
expect the replacement line to be the same length as the
original line - so the whole rest of the file needs to be rewritten
to accomodate that likely change anyway. (Indeed, with list-
directed writes, there's no reason to expect the same number
of records to be written each time either. Fortran's list-directed
output is expected to divide the output gracefully into as many
lines as the implementor chooses.)

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare

Herman D. Knoble

2007-04-18, 7:05 pm

Suggest you look at a decent text editor, like Kedit (not free, 2GB max file size) :
http://www.kedit.com/

VIM (free, 2GB max file size0 ): http://www.vim.org/download.php

Another editor that will edit up to 2GB t files and is free is XVI32:
http://www.chmaas.handshake.de/
It shows the file format in Hex and Text mode.

There are lots of other text editors of course.

Skip Knoble.


On Wed, 18 Apr 2007 14:49:57 GMT, "John S" <john_shaeffer@REMOVEearthlinkMe.net> wrote:

-|Due to historical reasons, have a long formated sequential (text) file of
-|data. Need to re-write one of the early header cards. Tried the open and
-|rewrite scheme below, but rest of file disappears after rewriting the
-|required line.
-|So, short of copying the rest of the file, how can one do this?
-|
-|File needs to be sequential & formated:
-|
-|The open is
-|
-|open( iU, file = cFileName )
-|write(iU,'(a)') ' VERSION_1 '
-|
-|write(iU,'(a)') ' some text'
-|
-|write(iU,'(79a)') cTitle
-|
-|
-|write(iU,*) rReal, iDum1, iDum2, iDum3
-|
-|write 1000's of lines of data.
-|
-|
-|
-|Now, need to change line 4 and keep all data following line 4. The
-|following deletes all data after line 4.
-|
-|rewind( iU )
-|
-|
-|do i = 1, 3
-|
-| read(iU,*)
-|
-|enddo
-|
-|
-|read(iU,*) rReal, iDum1, iDum2, iDum3
-|
-|backspace(iU)
-|
-|
-|iDum3 = iNewValue ! change parameter value
-|
-|
-|write(iU,*) rReal, iDum1, iDum2, iDum3
-|
-|close(iU)
-|
-|So, how to change only one line of this data file without reading /
-|rewriting the entire file.
-|
-|Thanks.
-|

Terence

2007-04-18, 7:05 pm

If you are SURE that the programs reading your new file will not be
affected by having trailing blank characters within a line, you can
replace a given line (or block of lines) with an equal length or
shorter length with cr-lf-ended space padding, at the front of the old
file (actually anywhere, but what you want is easier).

Of course, rewriting a file is much easier and less accident-prone.

You modify an existing text file by opening the file as unformatted
direct access, defiened with a block length of the length which must
be of the same record length as the part you are replacing, up to and
including the last cr-lf of your last line to be altered (or an exact
fraction of this and read/write the appropriate number of records)..

You prepare the new block, including all the required cr-lf line-end
defining characters and write this record block to replace the old
one, then close the file without using any end-file instruction.

I use this tecnique to change the default font definition and language
in RTF files, and similar file header change operations.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com