Solved

system independant file size calculation

Posted on 2001-07-17
5
376 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming on screen browser tutorial lesson 2 76
C++ mouse_event mouse look 7 70
max float value 3 25
Why isn't object file created? 6 42
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 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

21 Experts available now in Live!

Get 1:1 Help Now