[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Standard C++ file size???

Peter Olcott

10/5/2008 3:47:00 PM

Is there any standard C++ way to determine the size of a
file before it is read?


19 Answers

Victor Bazarov

10/5/2008 4:21:00 PM

0

Peter Olcott wrote:
> Is there any standard C++ way to determine the size of a
> file before it is read?

No. The "standard C++ way" is to open the file for reading, seek to the
end of the file and get the position. If you need the size of the file
on disk (and you have the name of the file) without "touching" is in any
way, use the existing platform (OS) mechanisms to get the "file stats"
(statistics). RTFM on programming your OS.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

utab

10/5/2008 7:59:00 PM

0

On Sun, 05 Oct 2008 10:46:43 -0500, Peter Olcott wrote:

> Is there any standard C++ way to determine the size of a file before it
> is read?

I guess the easiest would be to use the binary operation mode and try to
get the size in that mode.

Some binary file operations are treated in
http://www.cplusplus.com/doc/tutorial/...

with some examples, they can get you started, I suppose ;)

Peter Olcott

10/5/2008 9:54:00 PM

0


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:gcaphr$8km$2@news.datemas.de...
> Peter Olcott wrote:
>> Is there any standard C++ way to determine the size of a
>> file before it is read?
>
> No. The "standard C++ way" is to open the file for
> reading, seek to the end of the file and get the position.

My best guess is that this is exactly what I need. I want to
read in an ASCII text file into a single contiguous block of
memory.

It would seem that I could do this using the method you
propose, and use a std::vector<unsigned char> for the memory
block, resized to position + 1. I would also guess that this
same method may also work for any possible type of data. Of
course I am assuming that the data is being read in binary
mode, in each case.

> If you need the size of the file on disk (and you have the
> name of the file) without "touching" is in any way, use
> the existing platform (OS) mechanisms to get the "file
> stats" (statistics). RTFM on programming your OS.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Maxim Yegorushkin

10/6/2008 8:35:00 AM

0

On Oct 5, 10:54 pm, "Peter Olcott" <NoS...@SeeScreen.com> wrote:
> "Victor Bazarov" <v.Abaza...@comAcast.net> wrote in message
>
> news:gcaphr$8km$2@news.datemas.de...
>
> > Peter Olcott wrote:
> >> Is there any standard C++ way to determine the size of a
> >> file before it is read?
>
> > No.  The "standard C++ way" is to open the file for
> > reading, seek to the end of the file and get the position.
>
> My best guess is that this is exactly what I need. I want to
> read in an ASCII text file into a single contiguous block of
> memory.
>
> It would seem that I could do this using the method you
> propose, and use a std::vector<unsigned char> for the memory
> block, resized to position + 1. I would also guess that this
> same method may also work for any possible type of data. Of
> course I am assuming that the data is being read in binary
> mode, in each case.

In this case you don't need to know the size. It could be as simple
as:

#include <fstream>
#include <iterator>
#include <vector>

int main()
{
std::ifstream file("text.file");
std::vector<char> file_in_memory(
(std::istream_iterator<char>(file))
, (std::istream_iterator<char>())
);
// the file has been read into file_in_memory
}

However, if performance is paramount, or you need to know the exact
file errors, or the file is too big to fit into memory, you may like
to use your platform's native functions (like POSIX open(), fstat()
and mmap()).

--
Max

James Kanze

10/6/2008 10:21:00 AM

0

On Oct 5, 6:21 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Peter Olcott wrote:
> > Is there any standard C++ way to determine the size of a
> > file before it is read?

> No. The "standard C++ way" is to open the file for reading,
> seek to the end of the file and get the position.

That's a frequently used method, but it certainly isn't standard
C++. There's no guarantee that the position is convertable to
an integral type, and there's no guarantee that the integral
value means anything if it is.

In practice, this will probably work under Unix, and with binary
(but not text) files under Windows. Elsewhere, who knows?

> If you need the size of the file on disk (and you have the
> name of the file) without "touching" is in any way, use the
> existing platform (OS) mechanisms to get the "file stats"
> (statistics). RTFM on programming your OS.

Supposing, of course, that the system has some sort of request
for determining what you mean by file size. The most obvious
meaning is the number of bytes you will read before encountering
EOF. And as far as I know, Unix is the only system which has a
request which will return this. Another reasonable meaning is
the number of bytes the file occupies on the disk, but I don't
know of any system which has a request for this. (Unix
certainly doesn't.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Gennaro Prota

10/6/2008 11:02:00 AM

0

James Kanze wrote:
[file size]
> Another reasonable meaning is
> the number of bytes the file occupies on the disk, but I don't
> know of any system which has a request for this. (Unix
> certainly doesn't.)

I have never tried it, but I think a few math (and path manipulation),
using GetDiskFreeSpaceEx and GetDiskFreeSpaceA should do it for (recent)
Windows. There might be gotchas I'm not seeing offhand, though.

Hopefully as off-topic as occasionally tolerable,

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/b...
Do you need expertise in C++? I'm available.

Matthias Buelow

10/6/2008 1:12:00 PM

0

James Kanze wrote:

> Another reasonable meaning is
> the number of bytes the file occupies on the disk, but I don't
> know of any system which has a request for this. (Unix
> certainly doesn't.)

stat(), lstat(), fstat() will determine the number of blocks used.

PeteOlcott

10/6/2008 2:34:00 PM

0

On Oct 6, 5:21 am, James Kanze <james.ka...@gmail.com> wrote:
> On Oct 5, 6:21 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
> > Peter Olcott wrote:
> > > Is there any standard C++ way to determine the size of a
> > > file before it is read?
> > No.  The "standard C++ way" is to open the file for reading,
> > seek to the end of the file and get the position.
>
> That's a frequently used method, but it certainly isn't standard
> C++.  There's no guarantee that the position is convertable to
> an integral type, and there's no guarantee that the integral
> value means anything if it is.
>
> In practice, this will probably work under Unix, and with binary
> (but not text) files under Windows.  Elsewhere, who knows?

Why would it not work for Text files under Windows?
(I am only looking for the size that can be block read into memory)

>
> > If you need the size of the file on disk (and you have the
> > name of the file) without "touching" is in any way, use the
> > existing platform (OS) mechanisms to get the "file stats"
> > (statistics).  RTFM on programming your OS.
>
> Supposing, of course, that the system has some sort of request
> for determining what you mean by file size.  The most obvious
> meaning is the number of bytes you will read before encountering
> EOF.  And as far as I know, Unix is the only system which has a
> request which will return this.  Another reasonable meaning is
> the number of bytes the file occupies on the disk, but I don't
> know of any system which has a request for this.  (Unix
> certainly doesn't.)
>
> --
> James Kanze (GABI Software)             email:james.ka...@gmail.com
> Conseils en informatique orientée objet/
>                    Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Victor Bazarov

10/6/2008 2:55:00 PM

0

PeteOlcott wrote:
> On Oct 6, 5:21 am, James Kanze <james.ka...@gmail.com> wrote:
>> On Oct 5, 6:21 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>>
>>> Peter Olcott wrote:
>>>> Is there any standard C++ way to determine the size of a
>>>> file before it is read?
>>> No. The "standard C++ way" is to open the file for reading,
>>> seek to the end of the file and get the position.
>> That's a frequently used method, but it certainly isn't standard
>> C++. There's no guarantee that the position is convertable to
>> an integral type, and there's no guarantee that the integral
>> value means anything if it is.
>>
>> In practice, this will probably work under Unix, and with binary
>> (but not text) files under Windows. Elsewhere, who knows?
>
> Why would it not work for Text files under Windows?
> (I am only looking for the size that can be block read into memory)

There is a difference between the number of bytes in the file
(physically on the disk) and the number of bytes you get when you read
the file due to the translation happening for the sequence of CR-LF, and
I don't remember which way it goes, you either get more when you read or
when you store it on disk. If there are more characters in the disk
storage, then you should be OK since you're going to allocate more than
you will read, but if it's the other way around, you might be in for a
surprise...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

James Kanze

10/6/2008 3:14:00 PM

0

On Oct 6, 3:11 pm, Matthias Buelow <m...@incubus.de> wrote:
> James Kanze wrote:
> > Another reasonable meaning is
> > the number of bytes the file occupies on the disk, but I don't
> > know of any system which has a request for this. (Unix
> > certainly doesn't.)

> stat(), lstat(), fstat() will determine the number of blocks
> used.

So they do. (I didn't remember it from when I learned stat.
But that was some time ago.) They also return the block size,
so with a little bit of multiplication... (Of course, this
doesn't include the space actually taken up by the inode:-).
Or in the directory entry. As Gennaro pointed out, the
definition of size is a bit vague to begin with, and I'm sure
that with a little bit of effort, I can come up with one that no
system supports.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34