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

Question on file size

Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.
thanks in advance.
Nov 13 '05 #1
8 12442
in comp.lang.c i read:
Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.


there is no way to be certain, since padding is allowed in binary streams
and translations may be occurring for text streams. often the platform
does track a file's size exactly, in which case there is usually a function
available -- but that should be addressed in a group which caters to your
platform and/or implementation.

--
a signature
Nov 13 '05 #2
those who know me have no need of my name wrote:
Hi all, can someone please show me how to get the size of a file with ANSIC? both in text mode and binary mode.
there is no way to be certain, since padding is allowed in binary streams
and translations may be occurring for text streams. often the platform
does track a file's size exactly, in which case there is usually a

function available -- but that should be addressed in a group which caters to your
platform and/or implementation.


Tell us more about this padding in a binary stream. Is there some picky
stream <> file thing here? Note that the OP asked about a file.
Nov 13 '05 #3
On Sun, 7 Dec 2003 09:22:08 -0800
"osmium" <r1********@comcast.net> wrote:
those who know me have no need of my name wrote:
Hi all, can someone please show me how to get the size of a file
with ANSI
C? both in text mode and binary mode.


there is no way to be certain, since padding is allowed in binary
streams and translations may be occurring for text streams. often
the platform does track a file's size exactly, in which case there
is usually a
function
available -- but that should be addressed in a group which caters to
your platform and/or implementation.


Tell us more about this padding in a binary stream. Is there some
picky stream <> file thing here? Note that the OP asked about a file.


In C you use streams to access files. Due to the limitations of some
systems binary streams (or files) are allowed to have padding at the
end. The reason for this being that some file systems only store the
file length to the nearest block.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #4
Flash Gordon writes:
Tell us more about this padding in a binary stream. Is there some
picky stream <> file thing here? Note that the OP asked about a file.


In C you use streams to access files. Due to the limitations of some
systems binary streams (or files) are allowed to have padding at the
end. The reason for this being that some file systems only store the
file length to the nearest block.


So the file has two sizes. What the user wanted n, and what he got, m. And
m may be larger than n. For a file append to work it seems to me that EOF
has to be detected at n and not m. Right? So if he is at EOF he gets n.
So why can't he use ftell() to see where he is?

BTW, I mentioned the stream - file thing only to minimize the number of
potential messages.
Nov 13 '05 #5

On Sun, 7 Dec 2003, osmium wrote:

Flash Gordon writes:
Tell us more about this padding in a binary stream. Is there some
picky stream <> file thing here? Note that the OP asked about a file.
In C you use streams to access files. Due to the limitations of some
systems binary streams (or files) are allowed to have padding at the
end. The reason for this being that some file systems only store the
file length to the nearest block.


So the file has two sizes. What the user wanted n, and what he got, m.
And m may be larger than n.


....although it's much more likely that n is larger than m. Although
it's possible for m to be larger, on the kind of compressed file system
that was being discussed somewhere elsethread recently.
For a file append to work it seems to me that EOF
has to be detected at n and not m. Right? So if he is at EOF he
gets n.
No, the user gets m. He *wants* n, but he *gets* m, according to
the definitions for m and n you just wrote.
So why can't he use ftell() to see where he is?
ftell() on a binary stream returns the number of bytes read from
the beginning of the file. As I understand it, that means that

#include <stdio.h>
int main()
{
FILE *fp = fopen("something_that_wont_fail_to_open", "rb");
getc(fp); getc(fp); getc(fp);
printf("%ld\n", ftell(fp));
return 0;
}

is guaranteed to print "3", no matter how many physical bytes on
disk correspond to the first three characters of the abstract
file. E.g., if every other byte of a binary file is an invisible
checksum byte, then the system will have processed six bytes
instead of only three (yet still ftell(fp)==3). Contrariwise, if
the disk has been encoded very efficiently, maybe the three stream
bytes only take up 17 or 18 bits on disk (yet still ftell(fp)==3).
BTW, I mentioned the stream - file thing only to minimize the
number of potential messages.


Good idea. :-)

-Arthur

Nov 13 '05 #6
Alex <al*****@myrealbox.com> wrote:
Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.


The following code is (very) light modification of the K&R example:

#include <sys/stat.h>
long fsize(const char *const name)
{
struct stat stbuf;
if(stat(name, &stbuf) == -1)
return -1; // The file could not be accessed.
return stbuf.st_size;
}

Example of call to the function:

#include <stdio.h>
int main(int argc, char **argv)
{
printf("Size of file \"%s\" is: %i bytes\n", argv[0], fsize(argv[0]));
return 0;
}

This is portable to most systems, but it is not ANSI C. I know you asked
specificly for ANSI C, but this might do the job for you too.

/Andreas
Nov 13 '05 #7
Alex wrote:
Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.
thanks in advance.


The "file size" is an attribute of the file, and is independent of
text/binary mode.

Doing text file stuff fully portably in ANSI C is not easy, and even
binary access can be non-trivial on some esoteric systems. As you can
see from the discussion elsethread, even something as simple as getting
a file's size isn't easy to do fully portably with ANSI C :-(

However, the following should work on most systems (untested code):

==============

#include <stdio.h>
#include <stdlib.h>

/* filesize() returns -1 on error, filesize (>=0) on success,
* and calls exit(EXIT_FAILURE) when fclose() fails. */

long filesize(const char *filename)
{
int success;
long size;

FILE *f;

/* open the file for reading */
if (fopen(filename", "rb")==0)
return -1; /* open failed; bail out */

/* seek to end, then query file position */
success = fseek(f, 0, SEEK_END)==0 && (size = ftell(f))!=-1;

/* the file must be closed - even if feek() or ftell() failed. */
if (fclose(f)!=0)
{
/* Panic: fclose() failed. Returning anyway would leave the
* file opened, thus leaking resources.
* It's better to report this and bail out. */

fprintf(stderr,
"error in filesize(\"%s\"): fclose() call failed.", filename);
exit(EXIT_FAILURE);
}

return success ? size : -1;
}
======

Best regards,

Sidney

Nov 13 '05 #8
On Sun, 07 Dec 2003 13:16:59 +0000, Alex wrote:
Hi all, can someone please show me how to get the size of a file with ANSI
C? both in text mode and binary mode.
thanks in advance.


In a word, you can't.

The only really useable approach _in C_ is to simply read bytes until you
hit EOF and count as you go. Two problems here: first, in text mode,
there may be translations and second, the actual size (on disk) of the
file may not match the read number of bytes - which may matter, if, say,
you wanted to show a prompt along the lines of "This file has N bytes; is
there enough space on the target to continue? Y/n"

There's another issue which is more general, even if you use some
system-specific tool to do the job: how do you guarantee that the file
size doesn't change from the time you read it until the time you make use
of the information? Sure, C doesn't know about multitasking, but most
modern OSen do - and C knows diddly about file locking, so another process
could merrily add or delete data.
Nov 13 '05 #9

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

Similar topics

3
by: J Poirier | last post by:
Hi All, I'm hoping that someone might have some pointers or examples on how to proceed with a solution to the following problem: A test application, which produces a trace file, is being run...
3
by: pagates | last post by:
Hi All, This is a pretty simple question, but with possibly a complex answer. I was wondering if anybody had any opinions or links for "rules" as to when to use elements and when to use...
14
by: UJ | last post by:
What's the easiest way (without setting up a style sheet) to set the font for an area of text. I need to be able to specify the exact font and font size - not the 1-7 numbers IE uses. I want to be...
1
by: Marko Vuksanovic | last post by:
I used the following code for implementing a file upload progress indicator, using UpdateProgress Panel, though I have a problem that FileUpload.Has File always returns false. Any suggestions what...
5
by: Keith | last post by:
Hello all, I have a C# Windows Forms app. It is in namespace App.GUI. It builds to Nav.exe. I have entered an application level setting using the designer. Its type is string, name is "FOO"...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
3
by: windstorm | last post by:
int main( void ) { int fd,size,position; char buf; char lkl = "hello,world!"; fd = creat( "test",0644 ); size = write( fd,lkl,sizeof(lkl) ); close(fd); fd = open( "test",O_RDWR ); size =...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
1
by: pitjpz | last post by:
We have moved our Database to another server. The server it was on used SQL 4 and the new one its on now uses SQL5 the only problem we can find is that when you attempt to delete a record from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...
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.