file size

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2006
Posts: 2
Reputation: khot_anuradha is an unknown quantity at this point 
Solved Threads: 0
khot_anuradha khot_anuradha is offline Offline
Newbie Poster

file size

 
0
  #1
Feb 19th, 2006
hi,
i wanted to knoe that how to calculate the total size of file in C?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: file size

 
0
  #2
Feb 19th, 2006
open the file, seek to end, the get current file position. How to do that depends on C or C++. There are also other ways, such as stat(), win32 api GetFileSize(), and others.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: file size

 
0
  #3
Feb 19th, 2006
>open the file, seek to end, the get current file position
That's only guaranteed to be meaningful on files opened with a binary orientation. Even then there's a possibility that the size may be inaccurate. The only portable way to determine the working size of a file is to open the file and read it, from beginning to end.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: file size

 
0
  #4
Feb 19th, 2006
Originally Posted by Narue
>oThe only portable way to determine the working size of a file is to open the file and read it, from beginning to end.
bullpucky! prove it by example. use of standard library file i/o functions are guarenteed by the language to be portable across platforms that support them. Open the file in binary mode and reading the file from beginning to end in MS-Windows will result same file size as returned by moving the file pointer to end-of-file and getting the file position. Open the file in text mode and reading file file will result in incorrect file size, but setting file pointer to end-of-file will result in the same file size that is returned when the file is opened in binary mode.

Text and binary mode opens are only relevant on MS-DOS/MS-Windows platforms. Other platforms result in identical reads/writes.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: file size

 
0
  #5
Feb 19th, 2006
>use of standard library file i/o functions are guarenteed by the
>language to be portable across platforms that support them.
And conveniently enough the standard clearly states that arbitrary seeking on a text file is undefined.

>reading the file from beginning to end in MS-Windows will result
>in incorrect file size if the file is opened in text mode.
Perhaps if you want a byte count, but more commonly a character count is what people want. And if they want a byte count, they open the file as binary. So I say again, with complete confidence: The only way to portably get the size of a file is to read it from beginning to end and count what you're looking for.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: file size

 
0
  #6
Feb 19th, 2006
Originally Posted by Narue

>reading the file from beginning to end in MS-Windows will result
>in incorrect file size if the file is opened in text mode.
Perhaps if you want a byte count, but more commonly a character count is what people want. And if they want a byte count, they open the file as binary. So I say again, with complete confidence: The only way to portably get the size of a file is to read it from beginning to end and count what you're looking for.
Why do you assume you can read people's minds? The guy said he wants the file size, not the count of the number of characters in the file. If you want the character count they you are right -- the entire file has to be read. But that is not what "file size" means.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: file size

 
0
  #7
Feb 19th, 2006
>Why do you assume you can read people's minds?
I don't assume. That's why I corrected your assumption that an arbitrary seek would work.

>But that is not what "file size" means.
Hypocricy is unbecoming. If you don't see it, allow me to explain. You have no idea what the OP means by "total size of file", and neither do I. The two most likely interpretations are byte count and character count. You assumed byte count, but I didn't assume either and offered a portable solution that worked for both.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: file size

 
0
  #8
Feb 19th, 2006
google for "definition of file size" and your definition is not included

http://www.google.com/search?hl=en&l...fine:File+size
The amount of space that a file takes up when stored on disk. File size is usally measured in bytes, kilobytes (K), megabytes (MB) or gigabytes (GB).
http://www.bized.ac.uk/educators/16-...p_glossary.htm
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: file size

 
0
  #9
Feb 19th, 2006
>google for "definition of file size" and your definition is not included
Clearly you're not in the right frame of mind to see reason, so I'll waste no more time with you. To keep the thread clean for relevant discussion, please direct any further off-topic comments to my private message box.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: file size

 
0
  #10
Feb 19th, 2006
Originally Posted by Narue
To keep the thread clean for relevant discussion, please direct any further off-topic comments to my private message box.
Peace :lol:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC