|  | Size of a File |  | |
| | | Ashit Vora |  |
| Posted: Sun Sep 07, 2008 7:30 pm Post subject: Size of a File |  |
Hi, I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file. I couldn't find a proper way of doing it. What I was planning to do is... Open the requested file, default position is 1st byte. Now I use fseek() and move it till the send of the file. Than I use ftell() to get the current position in the file.
this should return the num of bytes (length / size) of the file.
Is this a proper way to do? Is there any efficient way to do so?
Thanks |
| |
| | | Daniel Kraft |  |
| Posted: Sun Sep 07, 2008 7:30 pm Post subject: Re: Size of a File |  |
Ashit Vora wrote:
| Quote: | Hi, I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file. I couldn't find a proper way of doing it. What I was planning to do is... Open the requested file, default position is 1st byte. Now I use fseek() and move it till the send of the file. Than I use ftell() to get the current position in the file.
|
That's what I would do, too (and what I was suggested to do when I had the same problem some time ago).
I don't know of any other way (to do it portably). There may be "better" tricks using platform specific APIs (like POSIX) though.
Daniel
-- Done: Arc-Bar-Cav-Sam-Val-Wiz, Dwa-Elf-Gno-Hum-Orc, Law-Neu-Cha, Fem-Mal To go: Hea-Kni-Mon-Pri-Ran-Rog-Tou |
| |
| | | Gordon Burditt |  |
| Posted: Sun Sep 07, 2008 7:30 pm Post subject: Re: Size of a File |  |
| |  | |
| Quote: | I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file.
|
First of all, you need to define what *IS* the size of a file. It's not a trivial question.
Is it the amount of space it takes on disk, including partially unused blocks? Does that include the size of a directory slot? Inode? Is it the number of bytes you can read from the file in binary mode? Is it the number of bytes you can read from the file in text mode? (This is likely to differ from the above on Windows systems with \r\n line endings). Is it the number of bytes that the UNIX "ls -l" or Windows "DIR" command returns for that file?
What bad things will happen if the size of the file changes between the time you compute the size of the file and the time you use that number (for what?)?
| Quote: | I couldn't find a proper way of doing it.
|
Depending on your definition of the size of a file, opening the file (in binary or text mode; which mode you use may affect the answer), and reading and counting characters will work.
| Quote: | What I was planning to do is... Open the requested file, default position is 1st byte. Now I use fseek() and move it till the send of the file.
|
Problem: a binary file need not meaningfully support seeking to the end of the file. In CP/M, for example, the size of a binary file is a multiple of the block size, and it doesn't keep track of how far into the last block you've written.
| Quote: | Than I use ftell() to get the current position in the file.
|
Problem: For a text file, the position need not be a number of anything. It could be a bitfield of sector, head, cylinder, track, train, disk number, etc. that has no correlation to a number of bytes.
| Quote: | this should return the num of bytes (length / size) of the file.
Is this a proper way to do?
|
It's not portable under ANSI C.
| Quote: | Is there any efficient way to do so?
|
There isn't a portable, efficient way to do so. stat(), if available, might be more efficient than your method. Opening the file, reading it, and counting bytes may be very inefficient. |
| |
| | | Lew Pitcher |  |
| Posted: Sun Sep 07, 2008 7:30 pm Post subject: Re: Size of a File |  |
| |  | |
On September 7, 2008 16:04, in comp.lang.c, Gordon Burditt (gordonb.7ma7q@burditt.org) wrote:
| Quote: | I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file.
First of all, you need to define what *IS* the size of a file. It's not a trivial question.
|
Agreed
| Quote: | Is it the amount of space it takes on disk, including partially unused blocks? Does that include the size of a directory slot? Inode? Is it the number of bytes you can read from the file in binary mode? Is it the number of bytes you can read from the file in text mode? (This is likely to differ from the above on Windows systems with \r\n line endings). Is it the number of bytes that the UNIX "ls -l" or Windows "DIR" command returns for that file?
|
Is it the count of the number of bytes stored in a sparse file (where there are areas of the file with no data at all) or is it the "virtual" size of the sparse file, where the intervening empty spots are presumed to have data? (Note, at least in unix, the "ls" command shows the "virtual" size, rather than a count of the real data written to a sparse file.)
[snip]
-- Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576 LINK | GPG public key available by request ---------- Slackware - Because I know what I'm doing. ------ |
| |
| | | Flash Gordon |  |
| Posted: Sun Sep 07, 2008 7:30 pm Post subject: Re: Size of a File |  |
| |  | |
Daniel Kraft wrote, On 07/09/08 20:52:
| Quote: | Ashit Vora wrote: Hi, I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file. I couldn't find a proper way of doing it. What I was planning to do is... Open the requested file, default position is 1st byte. Now I use fseek() and move it till the send of the file. Than I use ftell() to get the current position in the file.
That's what I would do, too (and what I was suggested to do when I had the same problem some time ago).
|
That is not a portable way to do it, and in any case it depends on what you mean by the size of a file.
| Quote: | I don't know of any other way (to do it portably). There may be "better" tricks using platform specific APIs (like POSIX) though.
|
I suggest both of you read question 19.12 of the comp.lang.c FAQ at LINK and the questions it links to. Also search this group for all the long discussions about this topic. Then ask if you need further information. -- Flash Gordon |
| |
| | | Guest |  |
| Posted: Sun Sep 07, 2008 8:15 pm Post subject: Re: Size of a File |  |
| |  | |
Ashit Vora wrote:
| Quote: | Hi, I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file. I couldn't find a proper way of doing it. What I was planning to do is... Open the requested file, default position is 1st byte. Now I use fseek() and move it till the send of the file. Than I use ftell() to get the current position in the file.
this should return the num of bytes (length / size) of the file.
Is this a proper way to do? Is there any efficient way to do so?
|
Unfortunately, this is problematic. There are two kinds of streams, binary and text.
7.19.9.2p3 says "A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END".
7.19.9.4p2 says, with regard to the ftell() function, that "For a text stream, its file position indicator contains unspecified information ...; the difference between two such return values is not necessarily a meaningful measure of the number of characters written or read."
Therefore, no matter what kind of stream you have, either the fseek() or the ftell() function is, at least in principle, unreliable for this purpose. In practice, for many implementations they will all work exactly as you expect. However, the only reasonably portable way to do it is to open the file in binary mode, read it a character at a time, keep count of how many characters have been red and wait for the end of file. Even this technique won't work if the stream that your reading isn't really a file, but is a special device.
There are more efficient ways of finding out the file size, but the appropriate method is different for different operating systems. On the systems I use most frequently, the relevant function is called stat(). It might be a very different function on your system.
Also, keep in mind that there are many different numbers that might be described as the size of a file (the amount of size it takes up on disk, the amount of data it contains, the compressed size, the uncompressed size, etc.), and different operating systems may give you access to numbers with different meanings. Also, if you're using a file whose size might change for reasons outside of your control during the time you're working on it, the value for the size that you get at one time might be out-of-date by the time you actually use that information. |
| |
| | | rio |  |
| Posted: Mon Sep 08, 2008 3:10 pm Post subject: Re: Size of a File |  |
"Gordon Burditt" <gordonb.7ma7q@burditt.org> ha scritto nel messaggio news:_-6dnTiv8s54rlnVnZ2dnUVZ_sLinZ2d@posted.internetamerica...
| Quote: | I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file.
What bad things will happen if the size of the file changes between the time you compute the size of the file and the time you use that number (for what?)?
|
easy i detect it and send an error message something as a=getsize(file) if((int)a<0) goto error; b=malloc(a+128) if(b==0) goto error; r=getfile(b, file, a+120) if(r==0) /* not get all the file until EOF */ goto error; |
| |
| | | Andrew Poelstra |  |
| Posted: Mon Sep 08, 2008 9:04 pm Post subject: Re: Size of a File |  |
| |  | |
On 2008-09-08, rio <a@b.c> wrote:
| Quote: | "Gordon Burditt" <gordonb.7ma7q@burditt.org> ha scritto nel messaggio news:_-6dnTiv8s54rlnVnZ2dnUVZ_sLinZ2d@posted.internetamerica... I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file.
What bad things will happen if the size of the file changes between the time you compute the size of the file and the time you use that number (for what?)?
easy i detect it and send an error message
|
It wasn't a challenge, it was a legitimate question about your implmentation - so not "easy" :-)
Whitespace? Fixed:
| Quote: | a = getsize(file) if(a < 0) goto error; b = malloc(a + 128) if(b == 0) goto error; r = getfile(b, file, a + 120)
if(r == 0) /* not get all the file until EOF */ goto error;
|
Well, this code still doesn't quite make sense. Maybe if I knew how getsize() and getfile() were defined it would.
-- Andrew Poelstra apoelstra@wpsoftware.com To email me, use the above email addresss with .com set to .net |
| |
| | | Barry Schwarz |  |
| Posted: Tue Sep 09, 2008 12:44 am Post subject: Re: Size of a File |  |
| |  | |
On Mon, 08 Sep 2008 23:04:41 GMT, Andrew Poelstra <apoelstra@supernova.home> wrote:
| Quote: | On 2008-09-08, rio <a@b.c> wrote:
"Gordon Burditt" <gordonb.7ma7q@burditt.org> ha scritto nel messaggio news:_-6dnTiv8s54rlnVnZ2dnUVZ_sLinZ2d@posted.internetamerica... I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file.
What bad things will happen if the size of the file changes between the time you compute the size of the file and the time you use that number (for what?)?
easy i detect it and send an error message
It wasn't a challenge, it was a legitimate question about your implmentation - so not "easy" :-)
something as
Whitespace? Fixed:
a = getsize(file) if(a < 0)
|
It's one thing to reformat code to make it readable but you really should include all the code from the original post. You left out a cast (which admittedly makes no sense but it was in the original).
| Quote: | goto error; b = malloc(a + 128) if(b == 0) goto error; r = getfile(b, file, a + 120)
if(r == 0) /* not get all the file until EOF */ goto error;
Well, this code still doesn't quite make sense. Maybe if I knew how getsize() and getfile() were defined it would.
|
-- Remove del for email |
| |
| | | Barry Schwarz |  |
| Posted: Tue Sep 09, 2008 12:44 am Post subject: Re: Size of a File |  |
On Mon, 8 Sep 2008 19:10:54 +0200, "rio" <a@b.c> wrote:
| Quote: | "Gordon Burditt" <gordonb.7ma7q@burditt.org> ha scritto nel messaggio news:_-6dnTiv8s54rlnVnZ2dnUVZ_sLinZ2d@posted.internetamerica... I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file.
What bad things will happen if the size of the file changes between the time you compute the size of the file and the time you use that number (for what?)?
easy i detect it and send an error message something as a=getsize(file) if((int)a<0) goto error;
|
What is a that you think casting it to an int will do any good?
| Quote: | b=malloc(a+128) if(b==0) goto error; r=getfile(b, file, a+120) if(r==0) /* not get all the file until EOF */ goto error;
|
-- Remove del for email |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|