P
Peter Olcott
Is there any standard C++ way to determine the size of a
file before it is read?
file before it is read?
Victor said: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.
Victor Bazarov said: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.
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.
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.)
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?
stat(), lstat(), fstat() will determine the number of blocks
used.
Why would it not work for Text files under Windows? (I am
only looking for the size that can be block read into memory)
James said: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() ;
Victor Bazarov said: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,
"Victor Bazarov" <[email protected]> wrote in message
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.
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
Peter said: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.
Which means?
If the requirements specification says to display the value
shown by Windows Explorer, fine. If the goal is to allocate a
buffer so you can read it in one go, it doesn't work. If the
goal is to know exactly how much space the file takes on the
disk, it doesn't work. As you said yourself, size is a rather
vague concept when it comes to files. Unless you're determining
the size so you can display it, in a way that is compatible with
Windows Explorer, then I don't see this working.
James Kanze wrote:
Considering the original question, what about this summary:
Q.: Is there any standard C++ way to determine the size of a file
without "reading" it?
A.: Not a strictly conforming one: the concepts of "file size" and
"file read" themselves, in fact, have no universal meaning; you'll
have to resort to an implementation-defined mechanism, if any,
such as stat() on POSIX platforms, GetFileSize()/GetFileSizeEx()
on Win32. The system documentation, or conformity to further
standards such as POSIX, may/should clarify what meaning of "size"
and/or "read" each of those mechanisms correspond to. This may not
apply to all of the supported file types.
Maybe this should be a FAQ (or two .
BTW, I do not think that anybody mentioned in this thread that in most
systems (i.e. any system that allows concurrent access to files), the
data returned by any kind of get file size API might be stale the
instant after it has been returned.
You can't really rely on it, except to treat it as some kind of hint.
Unless of course the OS gives you some way to acquire exclusive access
to that file before the get file size request.
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.