Question on file size

A

Alex

Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.
thanks in advance.
 
T

those who know me have no need of my name

in comp.lang.c i read:
Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.

there is no way to be certain, since padding is allowed in binary streams
and translations may be occurring for text streams. often the platform
does track a file's size exactly, in which case there is usually a function
available -- but that should be addressed in a group which caters to your
platform and/or implementation.
 
F

Flash Gordon

Tell us more about this padding in a binary stream. Is there some
picky stream <> file thing here? Note that the OP asked about a file.

In C you use streams to access files. Due to the limitations of some
systems binary streams (or files) are allowed to have padding at the
end. The reason for this being that some file systems only store the
file length to the nearest block.
 
O

osmium

those said:
there is no way to be certain, since padding is allowed in binary streams
and translations may be occurring for text streams. often the platform
does track a file's size exactly, in which case there is usually a function
available -- but that should be addressed in a group which caters to your
platform and/or implementation.

Tell us more about this padding in a binary stream. Is there some picky
stream <> file thing here? Note that the OP asked about a file.
 
A

Arthur J. O'Dwyer

So the file has two sizes. What the user wanted n, and what he got, m.
And m may be larger than n.

....although it's much more likely that n is larger than m. Although
it's possible for m to be larger, on the kind of compressed file system
that was being discussed somewhere elsethread recently.
For a file append to work it seems to me that EOF
has to be detected at n and not m. Right? So if he is at EOF he
gets n.

No, the user gets m. He *wants* n, but he *gets* m, according to
the definitions for m and n you just wrote.
So why can't he use ftell() to see where he is?

ftell() on a binary stream returns the number of bytes read from
the beginning of the file. As I understand it, that means that

#include <stdio.h>
int main()
{
FILE *fp = fopen("something_that_wont_fail_to_open", "rb");
getc(fp); getc(fp); getc(fp);
printf("%ld\n", ftell(fp));
return 0;
}

is guaranteed to print "3", no matter how many physical bytes on
disk correspond to the first three characters of the abstract
file. E.g., if every other byte of a binary file is an invisible
checksum byte, then the system will have processed six bytes
instead of only three (yet still ftell(fp)==3). Contrariwise, if
the disk has been encoded very efficiently, maybe the three stream
bytes only take up 17 or 18 bits on disk (yet still ftell(fp)==3).
BTW, I mentioned the stream - file thing only to minimize the
number of potential messages.

Good idea. :)

-Arthur
 
O

osmium

Flash said:
In C you use streams to access files. Due to the limitations of some
systems binary streams (or files) are allowed to have padding at the
end. The reason for this being that some file systems only store the
file length to the nearest block.

So the file has two sizes. What the user wanted n, and what he got, m. And
m may be larger than n. For a file append to work it seems to me that EOF
has to be detected at n and not m. Right? So if he is at EOF he gets n.
So why can't he use ftell() to see where he is?

BTW, I mentioned the stream - file thing only to minimize the number of
potential messages.
 
A

Andreas Kirkeskov Carlsen

Alex said:
Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.

The following code is (very) light modification of the K&R example:

#include <sys/stat.h>
long fsize(const char *const name)
{
struct stat stbuf;
if(stat(name, &stbuf) == -1)
return -1; // The file could not be accessed.
return stbuf.st_size;
}

Example of call to the function:

#include <stdio.h>
int main(int argc, char **argv)
{
printf("Size of file \"%s\" is: %i bytes\n", argv[0], fsize(argv[0]));
return 0;
}

This is portable to most systems, but it is not ANSI C. I know you asked
specificly for ANSI C, but this might do the job for you too.

/Andreas
 
S

Sidney Cadot

Alex said:
Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.
thanks in advance.

The "file size" is an attribute of the file, and is independent of
text/binary mode.

Doing text file stuff fully portably in ANSI C is not easy, and even
binary access can be non-trivial on some esoteric systems. As you can
see from the discussion elsethread, even something as simple as getting
a file's size isn't easy to do fully portably with ANSI C :-(

However, the following should work on most systems (untested code):

==============

#include <stdio.h>
#include <stdlib.h>

/* filesize() returns -1 on error, filesize (>=0) on success,
* and calls exit(EXIT_FAILURE) when fclose() fails. */

long filesize(const char *filename)
{
int success;
long size;

FILE *f;

/* open the file for reading */
if (fopen(filename", "rb")==0)
return -1; /* open failed; bail out */

/* seek to end, then query file position */
success = fseek(f, 0, SEEK_END)==0 && (size = ftell(f))!=-1;

/* the file must be closed - even if feek() or ftell() failed. */
if (fclose(f)!=0)
{
/* Panic: fclose() failed. Returning anyway would leave the
* file opened, thus leaking resources.
* It's better to report this and bail out. */

fprintf(stderr,
"error in filesize(\"%s\"): fclose() call failed.", filename);
exit(EXIT_FAILURE);
}

return success ? size : -1;
}


======

Best regards,

Sidney
 
K

Kelsey Bjarnason

Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.
thanks in advance.

In a word, you can't.

The only really useable approach _in C_ is to simply read bytes until you
hit EOF and count as you go. Two problems here: first, in text mode,
there may be translations and second, the actual size (on disk) of the
file may not match the read number of bytes - which may matter, if, say,
you wanted to show a prompt along the lines of "This file has N bytes; is
there enough space on the target to continue? Y/n"

There's another issue which is more general, even if you use some
system-specific tool to do the job: how do you guarantee that the file
size doesn't change from the time you read it until the time you make use
of the information? Sure, C doesn't know about multitasking, but most
modern OSen do - and C knows diddly about file locking, so another process
could merrily add or delete data.
 

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
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top