From: Adam Cutbill on
Hi,

Very simple/beginner question, but I'm having trouble finding a nice way to do this. I'm looking to check if a .txt is empty in append mode. I have a function that writes to the file, but if it's empty a header should be written first. I just need a good way to check if the file is empty.

Thanks a lot
From: dpb on
Adam Cutbill wrote:
> Hi,
>
> Very simple/beginner question, but I'm having trouble finding a nice way
> to do this. I'm looking to check if a .txt is empty in append mode. I
> have a function that writes to the file, but if it's empty a header
> should be written first. I just need a good way to check if the file is
> empty.

doc dir % look at structure components returned...

--
From: Walter Roberson on
Adam Cutbill wrote:

> Very simple/beginner question, but I'm having trouble finding a nice way
> to do this. I'm looking to check if a .txt is empty in append mode. I
> have a function that writes to the file, but if it's empty a header
> should be written first. I just need a good way to check if the file is
> empty.

There is a theoretical possibility that an "empty" text file could occupy disk
space. If you know that your operating system does not do that, then you could
use dir() on the file and check to see that the returned "size" field is non-zero.

If "empty" text files might occupy disk space (e.g., a header indicating the
character encoding in use) then you have a more obscure task: fseek() to
location 0 relative to the beginning of file, record ftell() of the file,
fseek() to location 0 relative to the end of file, record ftell() again, and
compare the two ftell values: if the two ftell values are the same then the
file is "empty" of text. You cannot simply fseek() to the end of file and
check to see if the ftell() value is non-zero, because ftell() on text files
(opened with a 't' permission) is "opaque", an arbitrary value of meaning only
to the operating system; this is unlike ftell() on _binary_ files, where the
ftell() value is a count of bytes relative to the beginning of the file.
From: Adam Cutbill on
Walter Roberson <roberson(a)hushmail.com> wrote in message <i17u0g$rn7$1(a)canopus.cc.umanitoba.ca>...
> Adam Cutbill wrote:
>
> > Very simple/beginner question, but I'm having trouble finding a nice way
> > to do this. I'm looking to check if a .txt is empty in append mode. I
> > have a function that writes to the file, but if it's empty a header
> > should be written first. I just need a good way to check if the file is
> > empty.
>
> There is a theoretical possibility that an "empty" text file could occupy disk
> space. If you know that your operating system does not do that, then you could
> use dir() on the file and check to see that the returned "size" field is non-zero.
>
> If "empty" text files might occupy disk space (e.g., a header indicating the
> character encoding in use) then you have a more obscure task: fseek() to
> location 0 relative to the beginning of file, record ftell() of the file,
> fseek() to location 0 relative to the end of file, record ftell() again, and
> compare the two ftell values: if the two ftell values are the same then the
> file is "empty" of text. You cannot simply fseek() to the end of file and
> check to see if the ftell() value is non-zero, because ftell() on text files
> (opened with a 't' permission) is "opaque", an arbitrary value of meaning only
> to the operating system; this is unlike ftell() on _binary_ files, where the
> ftell() value is a count of bytes relative to the beginning of the file.

Thanks Walter that helps a lot :). The file does have a header. I'll use the fseek method you described.