473,748 Members | 8,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Size of file

I hear that this isn't always valid:

FILE *in;
long size;
in = fopen("foo.bar" ,"rb");
fseek(in,0,SEEK _END);
size = ftell(in);
fseek(in,0,SEEK _SET);

then fread size many bytes into memory.

Apparently fseek is not guaranteed to work because of 0xFF EOF or other
characters, is this true only in text mode or also in binary mode? Is there
anyway to get a filesize without having to read bytes on at a time. Is it
best to just fread until it fails?
Oct 2 '08 #1
18 2831

"MisterE" <Mi*****@nimga. comwrote in message
news:48******** **************@ news.optusnet.c om.au...
>I hear that this isn't always valid:

FILE *in;
long size;
in = fopen("foo.bar" ,"rb");
fseek(in,0,SEEK _END);
size = ftell(in);
fseek(in,0,SEEK _SET);

then fread size many bytes into memory.

Apparently fseek is not guaranteed to work because of 0xFF EOF or other
characters, is this true only in text mode or also in binary mode? Is
there anyway to get a filesize without having to read bytes on at a time.
Is it best to just fread until it fails?
Works for me, using binary mode files. But there are various pitfalls:

In text mode, the size you get might be wrong because it might include '\n'
'\r' sequences instead of just '\n'.

Some types of files may not have a beginning or end (like stdin, or some
serial device), so don't have a size.

Some OSs may not store the exact bytesize of a file (for example may only
store a block size), so the value might be approximate. (And there might be
other OS things to bear in mind such as use of compression.)

And whatever file size you get might change if the file is modified (by any
other process) by the time you use the file size information.

For more details, see threads on this subject in c.l.c.

But within those constraints, I've been using code like yours successfully
for a decade or two.

--
Bartc

Oct 2 '08 #2
>I hear that this isn't always valid:

There are many, many, many different definitions of "file size",
(probably more than there are file sizes on a 64-bit machine) and
you need to decide which definition you want to use if you intend
calling any result "correct" or "incorrect" .
>FILE *in;
long size;
in = fopen("foo.bar" ,"rb");
fseek(in,0,SEE K_END);
size = ftell(in);
fseek(in,0,SEE K_SET);

then fread size many bytes into memory.
In binary mode, SEEK_END need not be meaningfully supported because
the system may pad the file with trailing 0 bytes. For example, CP/M
only counts sectors on binary files and rounds the size of the file
up to the next multiple of 128 bytes, and pads the last sector with
trailing 0 bytes.

In text mode, the size returned from ftell need not be meaningful as
a number. For example, it might be a bitfield of a number of values
like sector, head, cylinder, track, train, etc. so that subtracting
two of them does not give anything meaningful.

(Try, for example, subtracting 09302008 from 10022008, treating
them as decimal integers rather than dates, and try to make sense
out of the result that would indicate that they are 2 days apart.
The same kind of encoding can be done on text file offsets.)

Byte offsets into a text file are likely to be misleading because
of the \r\n -\n translation done by some systems (e.g. Windows).
>Apparently fseek is not guaranteed to work because of 0xFF EOF or other
characters,
There is no "EOF character". Even on one those systems which use
an end marker for text files (Windows), that marker isn't 0xFF.
Many systems (UNIX & variants) just store a file length (yet another
definition of "file size") and don't use an end marker.

EOF is a value that won't *fit* in a char (unless sizeof int ==
sizeof char) which is why getchar() returns int, not char.
>is this true only in text mode or also in binary mode?
You are screwed in both text mode and in binary mode for different
reasons.
>Is there
anyway to get a filesize
Do you want *A* filesize (in which case, I pick 0, it's easy, and
you didn't say it had to be correct, and some files actually do
have size 0) or do you want a *correct* filesize, in which case you
have to pick a definition of filesize?
>without having to read bytes on at a time. Is it
best to just fread until it fails?
If you want to read the file into memory, two definitions
of file size come to mind:

1. The number of bytes read from the file in binary mode.
2. The number of bytes read from the file in text mode.

Chances are high that these two definitions will give different
answers for the file size for any given file. Neither of these
necessarily says anything about how much space the file takes on
disk. But if you want to read the file into memory, these are
the right definitions to use (pick the one that uses the same
file mode as the file mode you're going to use).
Oct 3 '08 #3
On Fri, 3 Oct 2008 09:24:53 +1000, "MisterE" <Mi*****@nimga. com>
wrote:
>I hear that this isn't always valid:
You heard right.
>
FILE *in;
long size;
in = fopen("foo.bar" ,"rb");
You open the file in binary.
>fseek(in,0,SEE K_END);
The standard specifically states "A binary stream need not
meaningfully support fseek calls with a whence value of SEEK_END."
>size = ftell(in);
fseek(in,0,SEE K_SET);

then fread size many bytes into memory.

Apparently fseek is not guaranteed to work because of 0xFF EOF or other
I don't where you came up with this. 0xFF is not a special character
in a binary file. It could even be a normal printable character since
the standard does not mandate ASCII or EBCDIC. EOF is not a
character. It is a macro. It is entirely possible that the value
used in that macro is not representable as a char.
>characters, is this true only in text mode or also in binary mode? Is there
anyway to get a filesize without having to read bytes on at a time. Is it
best to just fread until it fails?
Depends on how important portability is to you.

--
Remove del for email
Oct 3 '08 #4
On Fri, 03 Oct 2008 09:24:53 +1000, MisterE wrote:
I hear that this isn't always valid:

FILE *in;
long size;
in = fopen("foo.bar" ,"rb");
fseek(in,0,SEEK _END);
size = ftell(in);
fseek(in,0,SEEK _SET);

then fread size many bytes into memory.
Most people use fopen and fseek. In my programs I used stat. One thing
that always made me wonder is that stat reports filesize == 0 if the file
is opened . Only on closed file it reports the correct size.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Gooogle Groups is Blocked. Reason: Excessive Spamming

Oct 3 '08 #5
arnuld <su*****@invali d.addresswrites :
>On Fri, 03 Oct 2008 09:24:53 +1000, MisterE wrote:
>I hear that this isn't always valid:

FILE *in;
long size;
in = fopen("foo.bar" ,"rb");
fseek(in,0,SEE K_END);
size = ftell(in);
fseek(in,0,SEE K_SET);

then fread size many bytes into memory.

Most people use fopen and fseek. In my programs I used stat. One thing
that always made me wonder is that stat reports filesize == 0 if the file
is opened . Only on closed file it reports the correct size.
Sorry this is becoming off-topic, but where did you find this
behavior? Under Unix this would be very strange.

The only thing I can think of is that you opened the file for writing,
which ordinarily would truncate it, so that its size would indeed be
0. But opening for reading should not do this.
Oct 3 '08 #6
On Thu, 02 Oct 2008 23:04:30 -0700, Nate Eldredge wrote:

Sorry this is becoming off-topic, but where did you find this
behavior? Under Unix this would be very strange.
well, it happens on my machine all the time.

The only thing I can think of is that you opened the file for writing,
which ordinarily would truncate it, so that its size would indeed be
0. But opening for reading should not do this.
fopen(file, "a")

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Gooogle Groups is Blocked. Reason: Excessive Spamming

Oct 3 '08 #7
arnuld <su*****@invali d.addresswrites :
>On Thu, 02 Oct 2008 23:04:30 -0700, Nate Eldredge wrote:

>Sorry this is becoming off-topic, but where did you find this
behavior? Under Unix this would be very strange.

well, it happens on my machine all the time.
What operating system / compiler / standard library?
>
>The only thing I can think of is that you opened the file for writing,
which ordinarily would truncate it, so that its size would indeed be
0. But opening for reading should not do this.

fopen(file, "a")
Peculiar. Can you post a complete example of a program that shows
this behavior?
Oct 3 '08 #8
arnuld wrote:
>MisterE wrote:
>I hear that this isn't always valid:

FILE *in;
long size;
in = fopen("foo.bar" ,"rb");
fseek(in,0,SEE K_END);
size = ftell(in);
fseek(in,0,SEE K_SET);

then fread size many bytes into memory.

Most people use fopen and fseek. In my programs I used stat. One
thing that always made me wonder is that stat reports filesize
== 0 if the file is opened . Only on closed file it reports the
correct size.
stat is not present in standard C. Thus it can do anything, and is
off topic here unless you present its actual coding (in standard
C).

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Oct 3 '08 #9
On 3 Oct 2008 at 7:35, CBFalconer wrote:
arnuld wrote:
>Most people use fopen and fseek. In my programs I used stat. One
thing that always made me wonder is that stat reports filesize
== 0 if the file is opened . Only on closed file it reports the
correct size.
I don't know exactly what you mean. Perhaps you're talking about writes
that might have been buffered and not yet actually made, which stat()
won't detect? For example:
#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
FILE *out;
struct stat buf;
out=fopen("foo" , "w");
if(out) {
fputs("12345", out);
if(stat("foo", &buf)==0)
printf("size: %lu\n", (unsigned long) buf.st_size);
fflush(out);
if(stat("foo", &buf)==0)
printf("flushed size: %lu\n", (unsigned long) buf.st_size);
fclose(out);
}
return 0;
}

$ ./a
size: 0
flushed size: 5
stat is not present in standard C. Thus it can do anything, and is
off topic here unless you present its actual coding (in standard
C).
Why don't you just crawl back in your hole and die if you don't have
anything useful to contribute?

Oct 3 '08 #10

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

Similar topics

17
15994
by: Arnold | last post by:
Is using fseek and ftell a reliable method of getting the file size on a binary file? I thought I remember reading somewhere it wasn't... If not what would be the "right" and portable method to obtain it? Thanks.
6
2481
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Hello all, I recall several threads over the years about how reading file size cannot be done consistantly or portably, but I don't remember any good reasons (not that I haven't read them, I'm sure, but it's more of a failure to hold them in my brain). Here is an attempt that I was commissioned to write, and I'd appreciate any comments and/or criticism (specific or...
8
564
by: Dave | last post by:
I am serialising an object to a memory mapped file (using the CreateFileMapping and MapViewOfFile p/invoke calls). These need to know the maximum size of the "file". I can put in a "good guess" ie it won't be more than, say, 1K, but it would be tidier to use the actaul size. Is it actually possible to find out how big an object (or even better, a class) would be when it is serialised (I suppose one way would be to separately serialise it...
8
21478
by: Ron | last post by:
Hi all, How do I determine the size of the tables I'm using? I looked under properties and it's not there. The book I just browsed said table is limited to 1GB. How do I find out what size my tables are? TIA ron
5
3737
by: Jefferis NoSpamme | last post by:
Hi all, I'm trying to limit the file size of an image submission and I keep running into various problems. I've got most of it working, but I'm stumped and I have a basic question as to WHY this works at all! if ($_FILES !="") { if ($_FILES<=0) { header("Location: /fileerror.php"); exit; }
12
15673
by: Phil Z. | last post by:
After migrating an ASP.NET 1.1 application to 2.0 we were getting "Cannot access a closed file" errors when uploading. I found a number of post on the subject and have since moved from using an Html File Input control to using the new FileUpload control. Previously, I had set the maxRequestLength attribute in the machine.config file to allow 100MB uploads with success. In the v2.0 application I have added an httpRuntime node to the...
4
3670
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating properly (I have a file that has a size of 1 KB but has 14 bytes in it so the conversion isn't working right). Is there some method/property out there that will get the actual size of the file? Also, would there be a method like this that will...
1
5457
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one group get automatically re-directed after uploading. However, this member group never gets the benefit of knowing if they've uploaded an incorrect file size or incorrect file extension. Members from the second group do see the "exceeds max file...
20
2497
by: Ashit Vora | last post by:
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.
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.