Get file byte size?

Discussion in 'C++' started by Immortal Nephi, Feb 14, 2010.

  1. Do C++ Standard Library have Get File Byte Size function in fstream
    header? If not, I have to use C Run-Time library instead.
     
    Immortal Nephi, Feb 14, 2010
    #1
    1. Advertisements

  2. Immortal Nephi

    Robert Fendt Guest

    And thus spake Immortal Nephi <>
    Sat, 13 Feb 2010 21:10:28 -0800 (PST):

    > Do C++ Standard Library have Get File Byte Size function in fstream
    > header? If not, I have to use C Run-Time library instead.


    You might try the following, without having it checked too thoroughly:

    stream.seekg(0, std::ios_base::end);
    std::streampos length = stream.tellg();
    stream.seekg(0);


    Regards,
    Robert
     
    Robert Fendt, Feb 14, 2010
    #2
    1. Advertisements

  3. Immortal Nephi

    James Kanze Guest

    On Feb 14, 5:10 am, Immortal Nephi <> wrote:
    > Do C++ Standard Library have Get File Byte Size function in
    > fstream header?


    No.

    > If not, I have to use C Run-Time library instead.


    It's not available in standard C, either.

    For starters: define what the function should return. Something
    like File Byte Size is very vague, and can mean several
    different things, and different things on different systems.
    The two "useful" meanings I can think of would be: how many
    bytes the file occupies on the disk, and how many bytes can I
    read from it sequentially before reaching end of file. For the
    first, as far as I know, neither Unix nor Windows provides a
    means of determining this. For the second, you can use stat
    under Unix, with reliable results. Under Windows, I'm not aware
    of anything which will tell you how many bytes you can read from
    an ifstream opened in text mode, other than actually reading it.
    (GetFileSize returns the number of bytes which can be read using
    ReadFile, which isn't the same thing.)

    --
    James Kanze
     
    James Kanze, Feb 14, 2010
    #3
  4. Immortal Nephi

    James Kanze Guest

    On Feb 14, 8:29 am, Robert Fendt <> wrote:
    > And thus spake Immortal Nephi <>
    > Sat, 13 Feb 2010 21:10:28 -0800 (PST):


    > > Do C++ Standard Library have Get File Byte Size function in
    > > fstream header? If not, I have to use C Run-Time library
    > > instead.


    > You might try the following, without having it checked too thoroughly:


    > stream.seekg(0, std::ios_base::end);
    > std::streampos length = stream.tellg();
    > stream.seekg(0);


    Well, it's certainly not guaranteed. For starters,
    std::streampos isn't even an integral type---it's a class. And
    there are very few guarantees regarding conversions of it to and
    from an integral type---std::streampos(0) is guaranteed to be
    the first position in the file, I think, but that's about it.

    In practice, this will work under Unix. Under Windows, it will
    usually result in a value that is somewhat larger than the file,
    if the file was opened as a text file.

    --
    James Kanze
     
    James Kanze, Feb 14, 2010
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tiewknvc9
    Replies:
    6
    Views:
    899
    Chris Uppal
    Oct 1, 2006
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    13,427
    Michael Jung
    May 25, 2008
  3. Mark
    Replies:
    5
    Views:
    508
  4. Keith Thompson

    Re: File Size - Big File Size

    Keith Thompson, Oct 1, 2009, in forum: C Programming
    Replies:
    6
    Views:
    491
    Phil Carmody
    Oct 3, 2009
  5. Michael Tsang

    Re: File Size - Big File Size

    Michael Tsang, Oct 4, 2009, in forum: C Programming
    Replies:
    2
    Views:
    493
    Keith Thompson
    Oct 4, 2009
  6. Nobody

    Re: File Size - Big File Size

    Nobody, Oct 5, 2009, in forum: C Programming
    Replies:
    10
    Views:
    2,416
    Flash Gordon
    Oct 10, 2009
  7. daved170

    read text file byte by byte

    daved170, Dec 12, 2009, in forum: Python
    Replies:
    30
    Views:
    2,343
    Nobody
    Dec 16, 2009
  8. John Joyce
    Replies:
    2
    Views:
    242
    John Joyce
    Apr 27, 2007
Loading...