Author |
Message |
Pete Clin #1 / 11
|
 size of UNIX files
This should be easy, but I've spent a while now probing the Sun AnswerBook and though I've probably missed the obvious, and I can't find a function that returns the size of a file. Can anyone enlighten me while I go get a caffeine injection... Thanks, Pete. -- Peter Clinch University of Dundee phone 44 382 60111 x 2050 Department of Medical Physics fax 44 382 640177 Ninewells Hospital
|
Sat, 04 May 1996 17:28:04 GMT |
|
 |
Andrew Tuck #2 / 11
|
 size of UNIX files
Quote:
>This should be easy, but I've spent a while now probing the Sun >AnswerBook and though I've probably missed the obvious, and I >can't find a function that returns the size of a file. Can >anyone enlighten me while I go get a caffeine injection...
--- code snippet on --- long filesize(FILE *fp) { long orig, size; orig = ftell(fp); fseek(fp, 0L, SEEK_END); size = ftell(fp); fseek(fp, orig, SEEK_SET); return(size); Quote: }
--- code snippet off --- This is the general idea. I don't have any books handy, but I think there is an ANSI library routine that does this as well. --
"C makes it easy to shoot yourself in the foot, C++ makes it harder, but when you do, it blows away your whole leg" -- Bjarne Stroustrup
|
Sat, 04 May 1996 23:08:38 GMT |
|
 |
Ken Naka #3 / 11
|
 size of UNIX files
Quote:
>This should be easy, but I've spent a while now probing the Sun >AnswerBook and though I've probably missed the obvious, and I >can't find a function that returns the size of a file. Can >anyone enlighten me while I go get a caffeine injection...
Try this: #include <stdio.h> long filesize(char* filename) { FILE* fp; long size; if ((fp = fopen(filename, "r")) == NULL) { return -1; } fseek(fp, 0L, SEEK_END); size = ftell(fp); fclose(fp); return size; Quote: }
The reason I prefer this to stat family system calls is that this function is much more portable than those system calls (you can use this filesize() in DOS environment or in even OS-9 environment which, IMHO, has `look-similar-to-UN*X-but-are-mangled-enough-to-make-you- confused' system calls). But if portability issue doesn't matter, stat family calls would be better because they may be more efficient than this. Of course you can use open(), lseek() and close() (you don't need to use tell() since lseek() returns current position) instead of fopen(), fseek(), ftell() and fclose() but I'm not sure if those low level I/O functions are defined in the standard C or not (I'm sure those are defined in POSIX.1, though). Hope this helps, Ken Nakata -- * I apologize if there are misused or impolite words or phrases in this mail or post. They are not intended; I don't fully understand certain words or nuances of phrasing. Any corrections of English composition or content would be greatly appreciated. Ken Nakata, CIS, NJIT *
|
Sat, 04 May 1996 23:16:11 GMT |
|
 |
Shimon Schreib #4 / 11
|
 size of UNIX files
Finding the size of a file from a c program: try: man stat. stat(path, &buf) uses a struct stat buf, to return data about the file. The element st_size is file size in bytes. --
Tel: Intl+972+4-313610 Disclaimer: This message will destroy itself in 5 seconds.
|
Sat, 04 May 1996 22:36:57 GMT |
|
 |
Dan P #5 / 11
|
 size of UNIX files
Quote: >#include <stdio.h> >long filesize(char* filename) >{ > FILE* fp; > long size; > if ((fp = fopen(filename, "r")) == NULL) {
| +---- this should be "rb" for portability Quote: > return -1; > } > fseek(fp, 0L, SEEK_END); > size = ftell(fp); > fclose(fp); > return size; >} >The reason I prefer this to stat family system calls is that this >function is much more portable than those system calls (you can use >this filesize() in DOS environment or in even OS-9 environment which, >IMHO, has `look-similar-to-UN*X-but-are-mangled-enough-to-make-you- >confused' system calls).
"much more portable" is a very good qualifier for this implementation, because it's not completely portable. ANSI allows files bigger than the capacity of a long int, which is what ftell returns. And I don't know if ANSI requires that the file position indicator for a binary file represents the offset in bytes from the beginning of the file. Quote: >But if portability issue doesn't matter, >stat family calls would be better because they may be more efficient >than this.
They _are_ more efficient, because they don't have to open the file. Quote: >Of course you can use open(), lseek() and close() (you don't need to >use tell() since lseek() returns current position) instead of fopen(), >fseek(), ftell() and fclose() but I'm not sure if those low level I/O >functions are defined in the standard C or not (I'm sure those are >defined in POSIX.1, though).
No, these functions are not in the ANSI library. They are in POSIX.1, but stat is there too, so... Dan -- Dan Pop CERN, L3 Experiment
Mail: CERN - PPE, Bat. 21 1-023, CH-1211 Geneve 23, Switzerland
|
Sun, 05 May 1996 02:59:18 GMT |
|
 |
Paul Schlyt #6 / 11
|
 size of UNIX files
> > #include <stdio.h> > > long filesize(char* filename) > { > FILE* fp; > long size; > > if ((fp = fopen(filename, "r")) == NULL) { > return -1; > } > fseek(fp, 0L, SEEK_END); > size = ftell(fp); > fclose(fp); > return size; > } > > The reason I prefer this to stat family system calls is that this > function is much more portable than those system calls (you can use > this filesize() in DOS environment or in even OS-9 environment which, > IMHO, has `look-similar-to-UN*X-but-are-mangled-enough-to-make-you- > confused' system calls). But if portability issue doesn't matter, > stat family calls would be better because they may be more efficient > than this. stat() and fstat() works fine at least in MS-DOS. However, in many cases neither stat/fstat or your filesize() above need to be used, since most major PC C compilers have a filesize() or filelength() function in their standard libraries. -- --- Paul Schlyter, Swedish Amateur Astronomer's Society (SAAF) Nybrogatan 75 A, 6 tr, S-114 40 Stockholm, Sweden
|
Sun, 05 May 1996 05:33:10 GMT |
|
 |
Robert Fri #7 / 11
|
 size of UNIX files
Quote:
>This should be easy, but I've spent a while now probing the Sun >AnswerBook and though I've probably missed the obvious, and I >can't find a function that returns the size of a file. Can >anyone enlighten me while I go get a caffeine injection... >Thanks, Pete. >-- >Peter Clinch University of Dundee >phone 44 382 60111 x 2050 Department of Medical Physics >fax 44 382 640177 Ninewells Hospital
Try 'man stat'. Robert
|
Sun, 05 May 1996 07:50:32 GMT |
|
 |
Lezz Gil #8 / 11
|
 size of UNIX files
If you're seriously interested in filesize, then you should remember that there can be a difference between the number of blocks used by a file and the offset to the end of a file. This happens when a file has 'holes' in it - i.e. there are parts of the file that have never been written to, and so there are no disk blocks assigned to that part of the file. This can happen in, for example, dbm or ndbm files. I _think_ that the size returned using the stat method will be the amount of memory actually used by the file, but I'm not sure. Lezz
|
Sun, 05 May 1996 09:57:57 GMT |
|
 |
Lawrence Kir #9 / 11
|
 size of UNIX files
Quote:
>I _think_ that the size returned using the stat method will be the >amount of memory actually used by the file, but I'm not sure.
stat returns the virtual size of the file (stored in the i-node in Unix) rather than the allocated size. -----------------------------------------
-----------------------------------------
|
Sun, 05 May 1996 18:53:06 GMT |
|
 |
Bob Goudre #10 / 11
|
 size of UNIX files
Quote:
>>I _think_ that the size returned using the stat method will be the >>amount of memory actually used by the file, but I'm not sure. >stat returns the virtual size of the file (stored in the i-node in Unix) >rather than the allocated size.
That's what the "st_size" field of struct stat contains. However, on BSD flavors of UNIX, there is also a field named "st_blocks" that can be used to determine how much disk space is actually consumed by the file, taking into account such things as file holes (which don't consume blocks that you otherwise might think they did) and indirect blocks (which consume space that you would otherwise not know about). ---------------------------------------------------------------------- Bob Goudreau Data General Corporation
+1 919 248 6231 Research Triangle Park, NC 27709, USA
|
Mon, 06 May 1996 02:40:54 GMT |
|
 |
Data Rentals and Sal #11 / 11
|
 size of UNIX files
Quote:
>long filesize(FILE *fp) { > long orig, size; > orig = ftell(fp); > fseek(fp, 0L, SEEK_END); > size = ftell(fp); > fseek(fp, orig, SEEK_SET); > return(size); >}
This is not guaranteed to be correct. -- #include <standard.disclaimer> _ Kevin D Quitt 91351-4454 96.37% of all statistics are made up
|
Mon, 06 May 1996 07:07:12 GMT |
|
|
|