• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

system independant file size calculation

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
BrianK
Asked:
BrianK
  • 2
  • 2
1 Solution
 
mnashadkaCommented:
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
 
BrianKAuthor Commented:
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
 
nietodCommented:
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
 
mnashadkaCommented:
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
 
nietodCommented:
>>  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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now