Solved

system independant file size calculation

Posted on 2001-07-17
5
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

690 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