473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Determining size of file

Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.

Thanks for your help.

Nov 30 '05 #1
14 2716
On 2005-11-30, googler <pi********@yahoo.com> wrote:
Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.

Thanks for your help.


Probably the best chance at getting a universally meaningful result is
eating the entire file with getc() and using ftell. That is likely not
the best solution. On those particular platforms, fseek() to the end
will probably work [there is an argument, based on the rationale, that
it will always give you at least as meaningful an answer as the getc()
loop], at least on a file opened in binary mode. Both also include
library functions that will give you the answer, but they are of course
different on each platform.
Nov 30 '05 #2
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-11-30, googler <pi********@yahoo.com> wrote:
Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.
Probably the best chance at getting a universally meaningful result is
eating the entire file with getc() and using ftell. That is likely not
the best solution. On those particular platforms, fseek() to the end
will probably work [there is an argument, based on the rationale, that
it will always give you at least as meaningful an answer as the getc()
loop], at least on a file opened in binary mode.
"A binary stream need not meaningfully support fseek calls with
a whence value of SEEK_END".

On Linux, there is no seek to end for /dev/random or /dev/zero
or sockets or named pipes; possibly not for tty's or ptty's either.
Reading to the end would work for sockets and named pipes... though if
you have two-way process communication going on then you might end up
with a logical deadlock... Some Unices support an indefinitely-extensible
mmap() segment, extended by trying to read the data there...
Both also include
library functions that will give you the answer, but they are of course
different on each platform.


If you don't know that what you've been handed is an "ordinary file"
then it can be difficult to define a meaningful "size" for it. But
of course there is nothing in the standard C library that would allow
you to test whether what you've been handed is an "ordinary file" or not.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 30 '05 #3
>Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.
What do you want to use the data for?
Thanks for your help.


*WHICH* "size of a given file"? There are several different definitions,
not all listed here, which are likely to give different answers:

(1) The number of characters you can read with fgetc() from the file
when it is opened in text mode. (On a system with line endings \r\n,
this counts \r\n as one character, but on disk it's two (MS-DOS and
Windows). If the file really contains binary stuff, on some systems
a control-Z is treated as end-of-file and it and the bytes after it
don't count. If you had ideas of reading the whole file into memory
in text mode, this is the size you want.

(2) The number of characters you can read with fgetc() from the file
when it is opened in binary mode. This may count bytes at the
end of the file from the last byte written to the end of a disk block
(e.g. on CP/M, which doesn't track end-of-file to a byte boundary).
If you had ideas of reading the whole file into memory in binary
mode, this is the size you want. It's also likely to be the size
given by "ls -l".

(3) The amount of disk space needed to store the file. This tends to
be the size of the data on disk rounded up to a block boundary.
There are variations on this as to whether to count "indirect"
blocks used to keep track of blocks belonging to the file.
Files with unwritten "holes" can make (3) drastically smaller
than (2) (e.g. the multi-gigabyte file with unwritten holes
that fits on a floppy).

(1) and (2) you can get by opening the file, reading it to the end,
and counting characters. (3) can be obtained by non-standard-C
function stat() on some systems (st_blocks multiplied by the disk
block size). stat() may often give you (2) in st_size also.

Gordon L. Burditt
Nov 30 '05 #4
On 2005-11-30, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-11-30, googler <pi********@yahoo.com> wrote:
Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.

Probably the best chance at getting a universally meaningful result is
eating the entire file with getc() and using ftell. That is likely not
the best solution. On those particular platforms, fseek() to the end
will probably work [there is an argument, based on the rationale, that
it will always give you at least as meaningful an answer as the getc()
loop], at least on a file opened in binary mode.


"A binary stream need not meaningfully support fseek calls with
a whence value of SEEK_END".

On Linux, there is no seek to end for /dev/random or /dev/zero
or sockets or named pipes; possibly not for tty's or ptty's either.


And those don't have sizes, so it all works out. If seek fails, the file
doesn't have a meaningful "size". though in some cases you might want
the number of bytes that can be read [a la "wc -c"]
Nov 30 '05 #5
googler wrote:

Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.


While not strictly C, as defined in this group, there are common POSIX
extensions for this. See the stat() function, which is available on
many platforms, including Linux and Windows.

If you have questions regarding stat(), you will need to take them to a
newsgroup for which it's not off-topic. (Perhaps comp.unix.programmer?)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 30 '05 #6
On 30 Nov 2005 12:22:30 -0800, in comp.lang.c , "googler"
<pi********@yahoo.com> wrote:
Is there any C library function that returns the size of a given file?


This is a FAQ
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 30 '05 #7
On 2005-11-30, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <sl********************@random.yi.org>,
Probably the best chance at getting a universally meaningful result is
eating the entire file with getc() and using ftell. That is likely not
the best solution. On those particular platforms, fseek() to the end
will probably work [there is an argument, based on the rationale, that
it will always give you at least as meaningful an answer as the getc()
loop], at least on a file opened in binary mode.


"A binary stream need not meaningfully support fseek calls with
a whence value of SEEK_END".


The argument i mentioned [based on the rationale] is that, at least in
the case of the padding bytes, such a seek should seek to the end
including the padding bytes on any reasonable system - for non-ordinary
streams a seek-to-end is no better in text mode than in binary mode, so
this clause isn't the one in effect in that case.

for another example stdin is a text stream, you can't seek to the end of
that on many implementations.
Nov 30 '05 #8

Jordan Abel wrote:
On 2005-11-30, googler <pi********@yahoo.com> wrote:
Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.

Thanks for your help.


Probably the best chance at getting a universally meaningful result is
eating the entire file with getc() and using ftell. That is likely not
the best solution. On those particular platforms, fseek() to the end
will probably work [there is an argument, based on the rationale, that
it will always give you at least as meaningful an answer as the getc()
loop], at least on a file opened in binary mode. Both also include
library functions that will give you the answer, but they are of course
different on each platform.


Thank you all. I used fseek(fp, 0, SEEK_END) followed by call to
ftell(). Works fine for both platforms and ordinary text files that I'm
dealing with.

Dec 1 '05 #9
Gordon Burditt wrote:
*WHICH* "size of a given file"? There are several different definitions,
not all listed here, which are likely to give different answers:

(1) The number of characters you can read with fgetc() from the file
when it is opened in text mode. (On a system with line endings \r\n,
this counts \r\n as one character, but on disk it's two (MS-DOS and
Windows). If the file really contains binary stuff, on some systems
a control-Z is treated as end-of-file and it and the bytes after it
don't count. If you had ideas of reading the whole file into memory
in text mode, this is the size you want.

(2) The number of characters you can read with fgetc() from the file
when it is opened in binary mode. This may count bytes at the
end of the file from the last byte written to the end of a disk block
(e.g. on CP/M, which doesn't track end-of-file to a byte boundary).
If you had ideas of reading the whole file into memory in binary
mode, this is the size you want. It's also likely to be the size
given by "ls -l".

(3) The amount of disk space needed to store the file. This tends to
be the size of the data on disk rounded up to a block boundary.
There are variations on this as to whether to count "indirect"
blocks used to keep track of blocks belonging to the file.
Files with unwritten "holes" can make (3) drastically smaller
than (2) (e.g. the multi-gigabyte file with unwritten holes
that fits on a floppy).


<OT>

This command will produce a file that is 92 KiB by (2), but almost 2 TiB
by (3).
dd seek=4091M count=1 if=/dev/zero of=big

[sbiber@eagle ~]$ du -h big
92K big
[sbiber@eagle ~]$ ls -lh big
-rw-rw-r-- 1 sbiber sbiber 2.0T Dec 2 01:36 big

Very easily fits on a floppy, and more than two thousand GiB. Of course,
the floppy's file system must support sparse files, which rules out
using the usual FAT.

</OT>

--
Simon.
Dec 1 '05 #10
Simon Biber wrote:
[... sparse files ...]
<OT>

This command will produce a file that is 92 KiB by (2), but almost 2 TiB
by (3).
dd seek=4091M count=1 if=/dev/zero of=big

[sbiber@eagle ~]$ du -h big
92K big
[sbiber@eagle ~]$ ls -lh big
-rw-rw-r-- 1 sbiber sbiber 2.0T Dec 2 01:36 big

Very easily fits on a floppy, and more than two thousand GiB. Of course,
the floppy's file system must support sparse files, which rules out
using the usual FAT.
I once thought of copy-protecting a program by taking advantage of
sparse files. Back in the days when 40MB (yes, "MB") was a lot, I
figured that writing data to some scattered bytes within a sparse
file, and verifying those bytes upon startup, would be a simple way
of doing this. After all, there was no way you could actually have
a 2GB file, so there was no simple way of copying it somewhere else.
</OT>


And, getting back to fseek(SEEK_END), let's not forget about sequential
access devices. (Does the standard have anything to say about such
things?) For example, pipes, ttys, and tape drives aren't known for
their rewind capabilities.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Dec 1 '05 #11
On 30 Nov 2005 16:43:50 -0800, in comp.lang.c , "googler"
<pi********@yahoo.com> wrote:
Thank you all. I used fseek(fp, 0, SEEK_END) followed by call to
ftell(). Works fine for both platforms and ordinary text files that I'm
dealing with.


Be careful that you do not allow users to tell your programme to read
from a pipe or tcp port such as chargen. How you do that is beyond the
scope of this group.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 1 '05 #12

Mark McIntyre wrote:
On 30 Nov 2005 16:43:50 -0800, in comp.lang.c , "googler"
<pi********@yahoo.com> wrote:
Thank you all. I used fseek(fp, 0, SEEK_END) followed by call to
ftell(). Works fine for both platforms and ordinary text files that I'm
dealing with.


Be careful that you do not allow users to tell your programme to read
from a pipe or tcp port such as chargen. How you do that is beyond the
scope of this group.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


I still have some doubt. It will be great if anybody can help.

Earlier (before adding code for finding file size), my code was
something like
fp = fopen(file_name, "a");
fprintf(fp, ...);

After adding two lines for getting file size, it changes to
fp = fopen(file_name, "a");
fseek(fp, 0, SEEK_END);
n = ftell(fp); /* n is used somewhere else */
fprintf(fp, ...);

I just want to know if this has the same effect as the earlier code
with regards to appending to the end of the file.

Thanks in advance.

Dec 2 '05 #13
On 1 Dec 2005 21:26:22 -0800, in comp.lang.c , "googler"
<pi********@yahoo.com> wrote:
After adding two lines for getting file size, it changes to
fp = fopen(file_name, "a");
This is opening the file in text mode. To reiterate:

For a text stream, its file position indicator contains unspecified
information, usable by the fseek function for returning the file
position indicator for the stream to its position at the time of the
ftell call; the difference between two such return values is not
necessarily a meaningful measure of the number of characters written
or read.

In other words, once opened in text mode, the result of ftell isn't
necessarily the file size. You can't rely on it.
fseek(fp, 0, SEEK_END);
Don't need this - opening a file for append necesssarily opens it with
the file pointer at the end.
I just want to know if this has the same effect as the earlier code
with regards to appending to the end of the file.


Yes.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 3 '05 #14
Mark McIntyre wrote:
On 1 Dec 2005 21:26:22 -0800, in comp.lang.c , "googler"
<pi********@yahoo.com> wrote:

After adding two lines for getting file size, it changes to
fp = fopen(file_name, "a");

This is opening the file in text mode. To reiterate:

For a text stream, its file position indicator contains unspecified
information, usable by the fseek function for returning the file
position indicator for the stream to its position at the time of the
ftell call; the difference between two such return values is not
necessarily a meaningful measure of the number of characters written
or read.

In other words, once opened in text mode, the result of ftell isn't
necessarily the file size. You can't rely on it.

fseek(fp, 0, SEEK_END);

Don't need this - opening a file for append necesssarily opens it with
the file pointer at the end.

I just want to know if this has the same effect as the earlier code
with regards to appending to the end of the file.

Yes.


No real mystery here is there? Assuming a DOS or Windows file system,
text lines are ended with two characters, CR and LF. C implementations
on these file systems determine to be Unixy and ignore the CR when
reading the text file but then putting it back when writing the file.

It is only this which accounts for the difference between file size and
how many bytes you can read before EOF.

On Unixy systems, there is no difference and file size and bytes read
are the same.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 3 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as...
2
by: Phil Galey | last post by:
In VB.NET I find the IO object very handy in replacing most of the functionality of the FileSystemObject. One exception, however, is in determining the size of a file. How can you determine the...
12
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
2
by: Phil Galey | last post by:
Using the following, you can determine the size of a file: Dim fi As New IO.FileInfo(<Path to file>) MsgBox(fi.Length) .... but what about the size of a directory? The IO.DirectoryInfo object...
2
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within dot net is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
13
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to...
0
by: Cameron Simpson | last post by:
On 10Jul2008 13:20, Manuel Vazquez Acosta <mva.led@gmail.comwrote: | Cameron Simpson wrote: | On 09Jul2008 15:54, Ethan Furman <ethan@stoneleaf.uswrote: | >The solution my team has used is to...
38
Frinavale
by: Frinavale | last post by:
I'm implementing a Silverlight application that uses Sockets to receive data that is pushed to it from a Socket Server. (Silverlight only supports the TCP protocol) The Socket Server pushes a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.