By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Question on file size

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.

--
a signature
Nov 13 '05 #2

P: n/a
those who know me have no need of my name wrote:
Hi all, can someone please show me how to get the size of a file with ANSIC? 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.


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.
Nov 13 '05 #3

P: n/a
On Sun, 7 Dec 2003 09:22:08 -0800
"osmium" <r1********@comcast.net> wrote:
those who know me have no need of my name wrote:
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.


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.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #4

P: n/a
Flash Gordon writes:
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.


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.
Nov 13 '05 #5

P: n/a

On Sun, 7 Dec 2003, osmium wrote:

Flash Gordon writes:
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.


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

Nov 13 '05 #6

P: n/a
Alex <al*****@myrealbox.com> wrote:
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
Nov 13 '05 #7

P: n/a
Alex wrote:
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

Nov 13 '05 #8

P: n/a
On Sun, 07 Dec 2003 13:16:59 +0000, Alex wrote:
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.
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.