Richard Maine <nos...@see.signature> wrote:
(snip)
>> > I suspect that you did not delete the old file before rewriting it. In
>> > that case, you are just overwriting the first 72 bytes of the old file,
>> > leaving the last ones as they were before. This, by the way, is exactly
>> > what C would also do in writing to an existing file.
(snip, then I wrote)
>> It won't normally do that. If you open the file to write:
>> out=fopen(file, "w");
>> then any existing file is truncated.
(snip on open with "r+")
> Well, Fortran won't "normally" do that either, as long as I'm allowed to
> define "normally" to mean whatever seems convenient to make the point at
> the time. In particular, I would "normally" open a file with
> status='replace' if I wanted to completely replace an old file.
In the early days of Fortran, it was much more common than now
to write a file, and then read it back in the same run of a
program. (And also, before the OPEN statement.)
I remember some complications with OS/360 related to the way
Fortran opened files. It opened them in either INOUT or OUTIN
(OS/360 terms). With the appropriate JCL option, you could convince
it to open files INPUT, which was sometimes important.
Also, there is no OS/360 JCL option that says to overwrite a file if
it exists, and create one if it doesn't.
In later years, and with languages developed later, it is more
usual to either read or write a file, but not both. The usual
C open options, "r" for read, "w" for write, make that easy,
including truncating existing files with "w". I believe C isn't
supposed to let you write to files opened "r", and the system I
tested it on didn't, but I couldn't be sure that no C systems
would do it.
Yes, open with "r+" will not truncate the file, allows you to
read or write to it. This is often used for direct access files
(which aren't special in C, other than the way you use them).
> The exact spellings of the C and Fortran statements are, of course
> different. But I repeat the contention that Fortran stream I/O was
> modelled after C. I *KNOW* that to be what it is modelled after; it
> isn't just a guess. I personally wrote the original proposal to add
> stream I/O to the Fortran standard, and I have a pretty good idea what I
> modelled it after. The formatted stream stuff was added after my
> original paper, but I helped work on that also and again, I know that it
> was intentionally modelled after C, particularly including the issue of
> file truncation, which is messier for the formatted case.
What you do with a file once opened seems to work much like C,
but the OPEN options haven't changed that much. I presume that
status='replace' works about the same for STREAM or non-stream
file opens.
Anyway, both Fortran and C have ways to open files for read/write,
that don't truncate on open and allow writing over parts of files.
Don't use those options if you don't want that effect.
-- glen