stat.st_size: How does it work?

P

Peter Lauren

Can anyone tell me roughly how stat.st_size works in order to get the
size of the file? Does it read through the file and count the number
of bytes? Would this take time for a super large file?

Thanks,
Peter.
 
L

Lew Pitcher

Can anyone tell me roughly how stat.st_size works in order to get the
size of the file?  Does it read through the file and count the number
of bytes?  Would this take time for a super large file?

IIRC, st_size is maintained as the file is written. With each write()
call, st_size is incremented by the number of bytes written.

So, when you stat() a file, st_size is just set, not computed.

HTH
 
L

Lew Pitcher

Oops... I didn't notice that this was comp.lang.c
Sorry for the off-topic reply.

To the OP: stat.st_size is AFAICR not part of the C standard.
Consequently, your question (and my reply) is off-topic here.

You really should ask in one of the forums dedicated to your operating
system (presumably a POSIX/SUS compatable OS such as BSD or Linux)
 
S

Seebs

Can anyone tell me roughly how stat.st_size works in order to get the
size of the file? Does it read through the file and count the number
of bytes? Would this take time for a super large file?

This has nothing at all to do with C, and a whole lot to do with whatever
programming environment you're looking at.

Just an idle thought, though: If you had to read through a file and
count bytes to know how large it was, how exactly would directory listings
work? I'd guess that filesystems probably track that kind of information
internally.

-s
p.s.: There is an eerie similarity between this "st_size" member which
supposedly contains the "size" of a file, and one of the components of the
Unix "struct stat" data object, in which case perhaps a Unix newsgroup?
Remember that the system call will behave the same way if you access it
from perl, or Ruby, or FORTRAN, or anything else; it's a feature of the
operating system, not of th eprogramming language.
 
L

luser- -droog

Implementation dependent. Also device dependent: you have to scan mag tape to
find out its length.

But for paper tape you could use a camera and trigonometry!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,952
Messages
2,570,111
Members
46,695
Latest member
Juliane58C

Latest Threads

Top