By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

filesize of an ofstream

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Fri, 07 May 2004 23:18:15 +0200, Rainer Goerke
<ra***********@epost.de> wrote in comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

P: n/a
On Fri, 07 May 2004 22:33:29 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
On Fri, 07 May 2004 23:18:15 +0200, Rainer Goerke
<ra***********@epost.de> wrote in comp.lang.c++:
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.


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
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3

P: n/a
Leor Zolman wrote in news:1k********************************@4ax.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

P: n/a
"Leor Zolman" <le**@bdsoft.com> wrote in message
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.
Jul 22 '05 #5

P: n/a
Jack Klein wrote:
On Fri, 07 May 2004 23:18:15 +0200, Rainer Goerke
<ra***********@epost.de> wrote in comp.lang.c++:

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.


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

Jul 22 '05 #6

P: n/a
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:40**************@sbcglobal.net...

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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.