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. Advertising

  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. Advertising

  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. Advertising

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. Andreas
    Replies:
    1
    Views:
    854
    Jonathan Bromley
    May 4, 2004
  2. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,256
    Roedy Green
    Aug 5, 2003
  3. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,584
    Michael Jung
    May 25, 2008
  4. Ron
    Replies:
    0
    Views:
    513
  5. daved170

    read text file byte by byte

    daved170, Dec 12, 2009, in forum: Python
    Replies:
    30
    Views:
    1,854
    Nobody
    Dec 16, 2009
Loading...

Share This Page