Solved

system independant file size calculation

Posted on 2001-07-17
5
375 Views
Last Modified: 2008-03-17
I need to be able to calculate file size. The code will be compiled on Linux, Irix, & Windows 2000/NT (and eventually Mac), all with different compilers.

this doesn't seem like it should be difficult, but I can't seem to get it working or find any examples of how it's done. I'm sure someone knows how...

my feeble attempts include:

ofstream f;
f.open(fileName);
f.seekp(0,ios::end);
cout << f.tellp() << endl;


and a couple other similar incarnations that aren't worth mentioning - essentially, I was trying to find the beginning & the end of the file & compare them somehow, but files aren't necessarily linear on the disk, so I abondoned that idea.

All files will be ASCII. Average file size will be somewhere in the neighborhood of 2-3MB, but it wouldn't be unheard of to deal with files as large as 50-60MB, so I want to stay away from any kind of counting. Number of characters per line will vary greatly, so I can't just make a guess based on averages. This needs to be moderately fast - the program will read series of these files, so a long calculation at the beginning of each one could turn out to be very expensive over several hundred or thousand files.

Accuracy isn't that incredibly important. I can probably be as much as 5% - 10% off in either direction without too much concern...

Thanks & regards,
-Brian
0
Comment
Question by:BrianK
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 100 total points
ID: 6291692
You were on the right track with ofstream, but ifstream is the file that you want (with seekg and tellg), like:
ifstream f(fileName);
f.seekg(0,ios::end);
cout << f.tellg() << endl;

Hope this helps you.
0
 

Author Comment

by:BrianK
ID: 6291738
Well, that does exactly what I needed.  Wouldn't have figured that ifstream or ofstream would have made a difference, but, in retrospect, it makes sense.

Thanks a bunch.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6292011
Your code with ofstream works fine.  but the ofstream class defaults to truncating the file when it opens it.  So the file length is 0.  Your code made the file length 0.   You can get around this by specifying a different open mode.

If you want a faster but non-portable solution you could use the GetFileAttributesEx() windows API function.  This is a windows function, not part of the standard C++.  But it will return the file size (and other inforamtion) without ever opening the file.  This is faster and allows you get the size of the file even when another processs has it open exclusively.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6292403
nietod, if you wanted non-portable, you should use stat (or _stat in VC++), since some form of it is found in all unix and windows compilers.  It also has less overhead (by not opening the file), and doesn't rely on Win32 system calls directly (although I'm sure the _stat is implemented that way).
0
 
LVL 22

Expert Comment

by:nietod
ID: 6293610
>>  in all unix and windows compilers
All UNIX implimentations yes, since it is part of the OS.  It is not part of the windows OS, so it is not avaialble on all windows compilers.  Some may choose to provide it for compatibility with UNIX, but there is nothing to force this.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now