473,406 Members | 2,956 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,406 software developers and data experts.

File size

*** 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 general) before I release it to my customer. Thanks!

Andrew

/* begin filesize.c */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main ( int argc, char *argv [] )
{
int status;

if ( 2 != argc )
{
fprintf ( stderr, "Usage: %s <filename>\n", argv [ 0 ] );
status = EXIT_FAILURE;
}
else
{
FILE *fp;

fp = fopen ( argv [ 1 ], "rb" );
if ( !fp )
{
fprintf ( stderr, "Cannot open file \"%s\" for reading.
[Error %d]\n", argv [ 1 ], errno );
status = EXIT_FAILURE;
}
else
{
char c;
long unsigned size = 0;

while ( fread ( &c, sizeof c, 1, fp ) )
{
size++;
}

fclose ( fp );
printf ( "Size of file (in bytes): %lu\n", size );
#if 0
if ( size > 1 << 10 )
{
/*** 1 KB ***/
printf ( "[%lu KB]\n", size / ( 1 << 10 ) );
}
#endif

status = EXIT_SUCCESS;
}
}

#if 0
printf ( "Returning status code: %d\n", status );
#endif
return status;
}

/* end filesize.c */
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

Nov 14 '05 #1
6 2457
"Andrew Clark" <an*****@syr.edu> wrote in message
news:Xn********************@208.33.61.211...
*** 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).
You don't have to know facts, you merely have to know where to find them. ;)
Here is an attempt that I was commissioned to write, and I'd appreciate
any comments and/or criticism (specific or general) before I release it to
my customer. Thanks!
If it's commercial code, what's preventing you from using some POSIX or other non-standard
but _much_ more practical method?
/* begin filesize.c */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main ( int argc, char *argv [] )
{
int status;

if ( 2 != argc )
{
fprintf ( stderr, "Usage: %s <filename>\n", argv [ 0 ] );
argv[0] may be "" or even NULL.
status = EXIT_FAILURE;
}
else
{
FILE *fp;

fp = fopen ( argv [ 1 ], "rb" );
if ( !fp )
{
fprintf ( stderr, "Cannot open file \"%s\" for reading.
[Error %d]\n", argv [ 1 ], errno );
status = EXIT_FAILURE;
}
else
{
char c;
long unsigned size = 0;
Files can be larger than unsigned long can represent. Hence the presence of fpos_t. You
should check for overflow of size. [Using two unsigned longs (64+ bits) should be enough
for the next couple of years though.]

while ( fread ( &c, sizeof c, 1, fp ) )
getchar() is likely to be much more efficient. Faster still is reading in larger chunks,
say 1024 bytes at a time.

But why bother reading the file at all? Why not just keep fseek-ing by large amounts?
{
size++;
}

You don't check for read errors. You may be misreporting the size.

Actually, even if you read the file without error, you have no guarantee that the file
will be the same size on subsequent reading.

Note that neither reading nor fseeking will work for streams which can't be rewound.
fclose ( fp );
printf ( "Size of file (in bytes): %lu\n", size );
#if 0
if ( size > 1 << 10 )
1024 is clearer to me than 1 << 10.
{
/*** 1 KB ***/
printf ( "[%lu KB]\n", size / ( 1 << 10 ) );
}
#endif

status = EXIT_SUCCESS;
}
}

#if 0
printf ( "Returning status code: %d\n", status );
#endif
return status;
}

/* end filesize.c */


--
Peter
Nov 14 '05 #2
In 'comp.lang.c', "Peter Nilsson" <ai***@acay.com.au> wrote:
while ( fread ( &c, sizeof c, 1, fp ) )


getchar() is likely to be much more efficient. Faster still is reading
in larger chunks, say 1024 bytes at a time.


You meant fgetc(), of course...

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #3
"Emmanuel Delahaye" <em**********@noos.fr> wrote in message
news:Xn***************************@212.27.42.73...
In 'comp.lang.c', "Peter Nilsson" <ai***@acay.com.au> wrote:
while ( fread ( &c, sizeof c, 1, fp ) )


getchar() is likely to be much more efficient. Faster still is reading
in larger chunks, say 1024 bytes at a time.


You meant fgetc(), of course...


Actually, I meant getc(), but the gist is the same. :-)

--
Peter
Nov 14 '05 #4

"Peter Nilsson" <ai***@acay.com.au> wrote in message
If it's commercial code, what's preventing you from using some
POSIX or other non-standard
but _much_ more practical method?
Maybe the customer wants portable ANSI C, or maybe this has been used as a
selling point.
Files can be larger than unsigned long can represent. Hence the
presence of fpos_t. You should check for overflow of size. [Using
two unsigned longs (64+ bits) should be enough
for the next couple of years though.]
And then we've all the fun of writing an int_to_ascii function, because two
longs can't be passed to printf().
But why bother reading the file at all? Why not just keep fseek-ing > by large amounts?

Unfortunately, if the file is text then fseek() / ftell() may not represent
the size. Here it is binary, so we don't have that problem, but there is
another issue with fseek() and ftell() not necessarily reporting the end of
the file.
That said, fseek()ing the end of the file and calling ftell() is a good
enough method for most practical purposes.
Nov 14 '05 #5
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c9**********@newsg3.svr.pol.co.uk...

"Peter Nilsson" <ai***@acay.com.au> wrote in message
If it's commercial code, what's preventing you from using some
POSIX or other non-standard
but _much_ more practical method?


Maybe the customer wants portable ANSI C, ...


Why would a client want ANSI C for _this_ task? What's stopping a programmer from
informing their client of better options?
Files can be larger than unsigned long can represent. Hence the
presence of fpos_t. You should check for overflow of size. [Using
two unsigned longs (64+ bits) should be enough
for the next couple of years though.]


And then we've all the fun of writing an int_to_ascii function, because two
longs can't be passed to printf().


Here's one to get you started...

char *ul2toa(char *s, unsigned long hi, unsigned long lo)
{
char *u, *v = s;
unsigned long q, r, d;

while (hi)
{
r = hi % 10;
hi = hi / 10;

d = (lo >> 16) + r * ((-1ul >> 16) + 1);
r = d % 10;
q = d / 10;

lo = (r << 16) + (lo & 0xFFFF);
r = lo % 10;
lo = lo / 10 + (q << 16);

*v++ = '0' + r;
}

do
{
*v++ = '0' + (lo % 10);
}
while (lo /= 10);

*v = 0;
for (u = s; u < --v; u++)
{ char c = *u; *u = *v; *v = c; }

return s;
}

--
Peter
Nov 14 '05 #6
"Peter Nilsson" <ai***@acay.com.au> wrote in message

Why would a client want ANSI C for _this_ task? [ file
size ] What's stopping a programmer from
informing their client of better options?
Let's say that the customer wants a suite of programs that use the stdin /
stdout model. Let's say that some are very processor intensive and they are
constantly investing in the latest hardware. If they have any sense they
will specify that all programs must be written in portable ANSI C, and
recompile the whole lot if new kit arrives.

Nov 14 '05 #7

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

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
4
by: M P | last post by:
Can you help me find an asp code that will upload a file from my PC to web server? Mark
11
by: Skc | last post by:
I have a .txt which has been exported as a .csv from an external source. What i need to do is to import this into SQL2000 (into a table) but I need to do special things on the data: 1. I need to...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: BW | last post by:
I am creating an upload/download function for an extranet site. Files will be uploaded to directory based upon the users login and associated project. The function works as long as I use "c:\Temp"...
2
by: Dan | last post by:
Hi, I know this code is not entirely javascript, but bare with me. Can you please tell me why this does not work: page: filemanager.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
1
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
2
by: cleary1981 | last post by:
Hi, I have created a script in PHP thats generates an SVG image. Want I want to do is have PHP save this as example.svg. Can this be done? Heres my code <?php require "config.php"; $proj_id =...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.