Board index » delphi » opening and closing files

opening and closing files

I wrote a small program for windoze (wincrt)
to edit my sig file so that my sig file would
change dayly.. It works great, but I've got a
little question.

I have a file called DATA.SS that holds information
such as possible random qoutes. I wanted to reset
the file pointer to the top of the file a couple
of times because I needed to make several runs
through the file. I simply reset() the file. I
did NOT close the file first though.

Do I _NEED_ to close the file when I only reset
it? Am I creating any new pointers or leaving
any files open?

Thanx,
Scott

 

Re:opening and closing files


Quote
Scott Dudley <sc...@nortexinfo.net> wrote:
>Do I _NEED_ to close the file when I only reset
>it? Am I creating any new pointers or leaving
>any files open?

No, Turbo Pascal uses the mode field of the file variable to determine the
current state of the file, and if it is open it is closed first.

This is true for Rewrite, Reset, and Append.  You can prove this for yourself by
invoking reset (append and/or rewrite) in 400 iterations of a for-loop. If the
file wasn't closed before being re-opened, you'd surely run out of handles :-)

    ...red

Re:opening and closing files


Quote
> I wrote a small program for windoze (wincrt)
> to edit my sig file so that my sig file would
> change dayly.. It works great, but I've got a
> little question.
> I have a file called DATA.SS that holds information
> such as possible random qoutes. I wanted to reset
> the file pointer to the top of the file a couple
> of times because I needed to make several runs
> through the file. I simply reset() the file. I
> did NOT close the file first though.
> Do I _NEED_ to close the file when I only reset
> it? Am I creating any new pointers or leaving any files open?

   Yes.  Don't do it that way.  Actually, if it's a reasonably small
file, why not read it once and store the data in memory (Heap, if
necessary) and access the data any way you want?  It'll save a lot of
overhead and eliminate file usage/handle problems you're no doubt
causing...

Re:opening and closing files


Quote
> >Do I _NEED_ to close the file when I only reset
> >it? Am I creating any new pointers or leaving
> >any files open?

> No, Turbo Pascal uses the mode field of the file variable to determine the
> current state of the file, and if it is open it is closed first.

> This is true for Rewrite, Reset, and Append.  You can prove this for yourself by
> invoking reset (append and/or rewrite) in 400 iterations of a for-loop. If the
> file wasn't closed before being re-opened, you'd surely run out of handles :-)

   Interesting.  I was sure I had gotten RTE errors when I inadvertently
tried to Reset a file I had previously opened with Reset but failed to
Close.  Even if this works, I feel it's a PPP to do so, to rely on the
RTL to do something like this for you...

Other Threads