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

filesize of an ofstream

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
6 7703
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
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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
// PROCESS XML CONTENT INTO DYNAMICALLY-NAMED ARRAYS foreach (array('mime', 'state', 'country') as $val) { $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);...
2
by: IWP506 | last post by:
I just CANNOT figure out what is wrong with this! ------ $op1f = fopen("./test.txt","r"); $op1 = fread($op1f, filesize($op1f)); ------
1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
2
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str();...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
4
by: Ken | last post by:
I would like to measure the fileSize of the image (without uploading it - php). I use: var size_pic = document.getElementById('num1').childNodes.fileSize; alert("size = " + size_pic); which...
3
by: Arjen | last post by:
Hello, When I have read a file with x.xxx.xxx.xxx bytes it is sometimes handy to show it in an diverent format than bytes. Is there a handy way to do this? Or is there a function for it? ...
5
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
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
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...
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
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...
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.