filesize of an ofstream

R

Rainer Goerke

Hi,

Is it possible in a simple way to get the size of the file "myfile.txt" of
the ofstream s:

ofstream s("myfile.txt", ios::app);

Thanks,
Rainer
 
J

Jack Klein

Hi,

Is it possible in a simple way to get the size of the file "myfile.txt" of
the ofstream s:

ofstream s("myfile.txt", ios::app);

Thanks,
Rainer

The only guaranteed, portable way to get the number of characters that
your program can read from a file is to read them and count them.

There might or might not be platform specific extensions that your
compiler/operating system combination supplies. You would need to ask
in a platform specific support group for that.

But even then, there is no guarantee that the value provided by that
function will equal the number of characters that you can read from
the file, especially in text mode.
 
L

Leor Zolman

The only guaranteed, portable way to get the number of characters that
your program can read from a file is to read them and count them.

There might or might not be platform specific extensions that your
compiler/operating system combination supplies. You would need to ask
in a platform specific support group for that.

But even then, there is no guarantee that the value provided by that
function will equal the number of characters that you can read from
the file, especially in text mode.

Hi Jack,
I'm sure this has come up a lot, but I don't have the complete picture in
my head (and scouring the library spec hasn't given me an answer yet), so
I'm wondering if I can impose on you to explain it in this context: If you
know a stream represents a binary file (let's not worry about text files
and the associated platform-specific CRLF handling), is there a
standard/portable way to perform the moral equivalent of:
1. ftell to get the current position
2. fseek to the end
3. ftell to get the file size
4. fseek back to where you started
in order to find out the file size? I found protected functions of
basic_filebuf to do various kinds of seeking, and rdbuf() to supposedly get
access to the basic_filebuf in order to try to invoke them, but I ran into
access violations trying to use those functions (they're protected, at
least in the Dinkumware implementation).
Thanks!
-leor
 
R

Rob Williscroft

Leor Zolman wrote in in
comp.lang.c++:
Hi Jack,
I'm sure this has come up a lot, but I don't have the complete picture
in my head (and scouring the library spec hasn't given me an answer
yet), so I'm wondering if I can impose on you to explain it in this
context: If you know a stream represents a binary file (let's not
worry about text files and the associated platform-specific CRLF
handling), is there a standard/portable way to perform the moral
equivalent of:
1. ftell to get the current position

This is the problem:
2. fseek to the end

Some systems don't keep accurate filesizes, i.e. a file will be a
whole number of disk-sectors (say). So seeking to the end can only portably
be done with text mode files, which presumably have an
EOF marker at the real end-of-file.
3. ftell to get the file size
4. fseek back to where you started

[snip]

I can't imagine there are any modern desktop OS's or server OS's
that don't allow you to do this, so as long as you don't want to
port to *every* C++ platform you should be able to do it. Just
don't do it in library code that someones going to port to your
microwave oven (it does have a hard disk doesn't it ? :).

#include <iostream>
#include <ostream>
#include <fstream>

int main()
{
using namespace std;

ifstream me( "test.cpp", ios::binary | ios::in );

me.seekg( 0, ios::end );

cout << me.tellg() << endl;
}

Rob.
 
S

Siemel Naran

Leor Zolman said:
1. ftell to get the current position
2. fseek to the end
3. ftell to get the file size

According to the standard C library, the combination of (2) and (3) is not
required to return the file size. The best way is usualyl to use some OS
function.
 
T

Thomas Matthews

Jack said:
The only guaranteed, portable way to get the number of characters that
your program can read from a file is to read them and count them.

Just to nitpick on you Jack, but we both know that the above algorithm
will return the number of {translated} characters in a file and not its
length. If the file is opened in binary, then it will be the number
of characters in a file. Some operating systems impose additional
overhead and bookkeeping in a file which adds to its length.

The number of characters in a file may not be the size of the entire
file.

An example: in MS-DOS systems, if I have a file containing the
sequence:
0x4a 0x61 0x63 0x6b 0x1a 0x20 0x4b 0x6c 0x65 0x69 0x6e
The file will hit EOF at the 5th byte because that is an end of
file marker, even though there is still data following it.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
M

Mike Wahler

Thomas Matthews said:
The number of characters in a file may not be the size of the entire
file.

An example: in MS-DOS systems, if I have a file containing the
sequence:
0x4a 0x61 0x63 0x6b 0x1a 0x20 0x4b 0x6c 0x65 0x69 0x6e
The file will hit EOF at the 5th byte because that is an end of
file marker, even though there is still data following it.

This will only occur with a stream opened in text mode on
that platform.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top