Checking file sizes 
Author Message
 Checking file sizes

Greetings...
    I may have been looking at the source and K&R too long, but for some
reason, I can't find an easy way to retrieve the size of a file. I use
QuickC (IBM PC). Anything ANSI standard would be appreciated, but if it's
"tweakable", that would be great, too.

Thanks...
   Bert

 * Origin: Hillbilly Korner BBS - (501)751-3714 (1:391/1160)



Sat, 17 Sep 1994 03:18:00 GMT  
 Checking file sizes

fp = fopen(file, "r");
fseek(fp, 0L, 2);
size = ftell(fp);

or

struct stat buf;

stat(file, &buf);
size = buf.st_size;

--Chris



Sat, 17 Sep 1994 23:46:52 GMT  
 Checking file sizes

Quote:

> Greetings...
>     I may have been looking at the source and K&R too long, but for some
> reason, I can't find an easy way to retrieve the size of a file. I use
> QuickC (IBM PC). Anything ANSI standard would be appreciated, but if it's
> "tweakable", that would be great, too.

> Thanks...
>    Bert

>  * Origin: Hillbilly Korner BBS - (501)751-3714 (1:391/1160)

I don't know how well this transports to Quick C: in Unix I have used

FILE *f;
long length;

f = fopen(....);
fseek(f, 0L, 2);        /* position at 0 bytes (0L) from end of file (2) */
length = ftell(f);      /* return where we are now */

and then re fseek() to go back to the beginning.
It doesn't seem to read the whole file in this process -
the file I'm checking is 66 MB long and it does it instantaneously.

Tim Kingsmill-Vellacott
Atmospheric, Oceanic and Planetary Physics
University of Oxford



Sun, 18 Sep 1994 00:26:57 GMT  
 Checking file sizes

Quote:
(Chris Webster) writes:
>fp = fopen(file, "r");
>fseek(fp, 0L, 2);
>size = ftell(fp);
>or
>struct stat buf;
>stat(file, &buf);
>size = buf.st_size;

Note that while both of these may very well work with QuickC, there is
no standard, portable way to determine the size of a file (other than
opening it and reading to EOF, I suppose.  Even then, you have to
worry about MS-DOG's "ascii" and "binary" modes of reading.  VMS
probably throws a few twists with the different file formats, too.)

Ftell is guaranteed only to return a "magic cookie" that will work with
fseek; it does not have to have any physical meaning.  And stat is available
only on Unix systems and systems which emulate the Unix C library.

--
David F. Skoll



Sun, 18 Sep 1994 03:06:42 GMT  
 Checking file sizes

Quote:
>>     I may have been looking at the source and K&R too long, but for some
>> reason, I can't find an easy way to retrieve the size of a file. I use
>> QuickC (IBM PC). Anything ANSI standard would be appreciated, but if it's
>> "tweakable", that would be great, too.
>[Uses this under unix}
>FILE *f;
>long length;
>f = fopen(....);
>fseek(f, 0L, 2);    /* position at 0 bytes (0L) from end of file (2) */
>length = ftell(f);  /* return where we are now */

Be aware this is not guaranteed to work.  The return value of ftell and
the second argument to fseek are not required to byte-counts from any
particular point; it's allowed for these values to be magic cookies.  The
only requirement is that fseek() will take the result of an ftell() and
return to that spot.
--

University of Kentucky                                  +1 606 257 2975
Department of Mathematics
                                              Flame early, flame often.


Sun, 18 Sep 1994 11:16:55 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. HELP - How to check file size in DOS

2. checking file size

3. Check the file size and number symbols in a file using C in Unix

4. FILE SIZE CHECKING FROM C PROGRAM

5. CHttpRequestParams::Render buffer size check is off by one

6. Memory size check

7. How to Check for size and create new db with ODBC

8. Checking size of array argument

9. How to check the size of an array when passed to a function

10. Array size checks

11. How to check the Hard Disk Size?

12. How to check the Hard disk size?

 

 
Powered by phpBB® Forum Software