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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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 youassume 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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
To keep the thread clean for relevant discussion, please direct any further off-topic comments to my private message box.
Peace :lol:
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Yes, the size of the file does not necessarily reflect the amount of data a program has actually stored in it -- there are win32 api functions that can increase the file size to an arbitrary size without doing any writes at all. I don't know if *nix or MAC has similar functions.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343