473,564 Members | 2,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Standard C++ file size???

Is there any standard C++ way to determine the size of a
file before it is read?
Oct 5 '08 #1
20 3529
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
Oct 5 '08 #2
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/files.html

with some examples, they can get you started, I suppose ;)
Oct 5 '08 #3
Victor Bazarov 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. 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.
Oct 5 '08 #4

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:gc******** **@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

Oct 5 '08 #5
On Oct 5, 10:54*pm, "Peter Olcott" <NoS...@SeeScre en.comwrote:
"Victor Bazarov" <v.Abaza...@com Acast.netwrote in message

news:gc******** **@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.
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<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()).

--
Max
Oct 6 '08 #6
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:ja******* **@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
Oct 6 '08 #7
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 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.
Oct 6 '08 #8
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.

Oct 6 '08 #9
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
Oct 6 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
2012
by: Matt | last post by:
I want to know what is the latest C standard version? Is it C99? There are many terms I have heard, including C98, C99, C9X. Or should we call ANSI/ISO C? Please advise. Thanks!!
20
3140
by: Chor Lit | last post by:
Hi, I asked Bjarne Stroustrup about the idea of adding colour standard for C++, and he said that it is very difficult for compiler vendors to change their IDE. But do you think it is possible ? Note that the proposed colour standard is not just merely to ease the eye only as what presently is in C++ compilers, but to aid in syntax...
270
9310
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting to see that the answers to that message prove that programming exclusively in standard C is completely impossible even for a small and ridiculously...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.