*** 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! =----- 6 2456
"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
"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
"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.
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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
|
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...
|
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....
|
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"...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |