Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

system independant file size calculation

Posted on 2001-07-17
5
Medium Priority
?
384 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

610 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