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
>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.
Note that "file size" is a less trivial notion than it might naively
appear: is it the number of bytes allocated on disk? The number of
characters you can read from the file in text mode? The number of bytes
you can read in binary mode? And what about symbolic links?
As Victor said, your platform is likely to expose a suitable function
yielding the number which corresponds to one particular definition of
"size" for the elements it can be applied to. POSIX systems must have
stat --note that this, modulo platform-specific extensions, doesn't know
what "size" is for some file types--; Win32 has GetFileAttribut esEx and
GetFileAttribut es, etc. Variants for "large file support" (e.g. stat64)
are also common.
Of course, Boost.Filesyste m may provide what you need just out of the
box.
--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
"Victor Bazarov" <v.Abazarov@com Acast.netwrote 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<uns igned charfor 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
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<uns igned charfor 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:
int main()
{
std::ifstream file("text.file ");
std::vector<cha rfile_in_memory (
(std::istream_i terator<char>(f ile))
, (std::istream_i terator<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()).
On Oct 5, 6:21 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
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.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
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 GetDiskFreeSpac eEx and GetDiskFreeSpac eA 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/breeze/>
Do you need expertise in C++? I'm available.
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.
On Oct 6, 5:21 am, James Kanze <james.ka...@gm ail.comwrote:
On Oct 5, 6:21 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>
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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Oct 6, 5:21 am, James Kanze <james.ka...@gm ail.comwrote:
>On Oct 5, 6:21 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>>
>>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
On Oct 6, 3:11 pm, Matthias Buelow <m...@incubus.d ewrote:
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.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Oct 6, 4:33 pm, PeteOlcott <PeteOlc...@gma il.comwrote:
On Oct 6, 5:21 am, James Kanze <james.ka...@gm ail.comwrote:
On Oct 5, 6:21 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
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)
void
readAll(
char const* filename )
{
std::ifstream f( filename ) ;
if ( ! f ) {
throw "cannot open" ;
}
f.seekg( 0, std::ios::end ) ;
if ( ! f ) {
throw "seek error" ;
}
long long size = f.tellg() ;
std::cout << filename << ": size = " << size << std::endl ;
if ( size != 0 ) {
f.clear() ;
f.seekg( 0, std::ios::beg ) ;
if ( ! f ) {
throw "rewind failed" ;
}
std::vector< char v( size ) ;
f.read( &v[ 0 ], size ) ;
if ( ! f ) {
throw "read failed" ;
}
}
}
int
main( int argc, char** argv )
{
for ( int i = 1 ; i != argc ; ++ i ) {
try {
readAll( argv[ i ] ) ;
} catch ( char const* error ) {
std::cout << argv[ i ] << ": " << error << std::endl ;
}
}
return 0 ;
}
Compile and try it on some text files. On a variant with some
extra comments, reading the source itself, I get:
readall.cc: size = 1677
under Solaris (g++ or Sun CC), but
readall.cc: size = 1733
readall.cc: read failed
under Windows (compiled with VC++).
If I open the file in binary mode, or use system level requests,
of course, I can make it work.
--
James Kanze (GABI Software) email:james.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
>>>>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)
>
Because it doesn't. Try it:
>
#include <iostream>
#include <fstream>
#include <vector>
>
void
readAll(
char const* filename )
{
std::ifstream f( filename ) ;
if ( ! f ) {
throw "cannot open" ;
}
f.seekg( 0, std::ios::end ) ;
if ( ! f ) {
throw "seek error" ;
}
long long size = f.tellg() ;
I think Victor meant that everything stopped here. Yes, the size so
obtained will happily count some garbage as well, and it's not likely
that read() will work with it, but at least that's the number you should
see in the Windows Explorer. In many cases that's all that is needed to
avoid a lot of user complaints :-)
PS: of course, too, the match with Explorer properties and everything I
say above is all a big dance of "likely", "perhaps" and "should be";
nothing, as you mentioned, is really guaranteed.
--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
"Victor Bazarov" <v.Abazarov@com Acast.netwrote in message
news:gcd8s4$svi $1@news.datemas .de...
PeteOlcott wrote:
>On Oct 6, 5:21 am, James Kanze <james.ka...@gm ail.com>
>wrote:
>>On Oct 5, 6:21 pm, Victor Bazarov
>><v.Abaza...@c omAcast.netwrot e:
>>>
>>>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,
I am talking about reading a Text file in binary mode so
there is no translation. I am making a computer language
compiler so my lexical analyzer will treat the text as
binary data.
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
Comment