On Sun, 30 Nov 2014 06:14:33 -0500, Harry Potter <
rose.j...@yahoo.com>
wrote:
> I want to read the file and keep it open, then go to the beginning of
> the file and re-write the file and end it there if it's smaller, then
> close the file.
There might be a valid way to re-position the end-of-file for an updated
file in ANSI C. I'd be concerned that it wasn't safe to do or used
undefined or host-specific behavior. Also, I'm not sure how to do this ...
It seems I've never needed to know how to do this, which implies to *me*
it's most likely not something which is needed, and therefore also likely
*not* something which is available, but I don't for sure.
You could experiment to see if ftell() changes after your seek to zero
and updates. I doubt it will. My understanding is that it shouldn't.
Append and update modes allow you to add more data, but AFAIK there is
nothing to reduce the data size of the file. I.e., the old data should
still be present if it exceeds the size of the newer data. If ftell()
does change and the file size becomes smaller, then the implementation
of the file routines for your C are updating the end-of-file. Even if
it does, I would suspect this to be non-standard behavior. Obviously,
I clearly can't confirm with a high level of certainty on this.
I would probably close the file after the read. Then, write out a new
file with the changes. If the new file is to be discarded, delete it.
If the new file is to be kept, delete the old, and rename the new. I'd
do that to ensure the correct data is in the file once it's closed. This
might not be workable for your situation. See comments further below.
> I want to keep it open to avoid corruption caused by two programs
> using the file at the same time.
Why do two programs access the file at the same time? In this situation,
it's usually only safe to allow multiple access if there are no writes or
updates to the file, i.e., both apps are _only_ *reading* the file.
> [...] and re-write the file [...]
> I want to keep it open to avoid corruption caused by two programs using
> the file at the same time.
Won't re-writing the file, "corrupt" the file for the second program? ...
The second program is unlikely to be updated with the changes, unless the
first program flushes and the second program re-reads all re-written data.
Obviously, my suggestion to use multiple files isn't going to work, or
might not work portably or reliably, if you've got multiple applications
accessing the same file at the same time, _unless_ both *only* read.
The writing and updating is an issue.
Does the second application only need the original data?
Does the second application need the written updates to the file?
Personally, I see this issue as a structuring problem. I.e., you need
to change the way you're doing something in order to not do so much to
the one file by so many things, e.g., either not update the file until
some later point in time such as after the second program is done with
it, or not access the file by multiple programs, or use multiple files,
etc.
Rod Pemberton