Re: File Size - Big File Size

Discussion in 'C Programming' started by Keith Thompson, Oct 1, 2009.

  1. "CapCity" <> writes:
    > Not a terrbily accomplished C coder here. I have a small ANSI C app that at
    > some point needs to know the number of bytes in a file. I've seen a few
    > suggestions along the lines of:
    >
    > fseek(fIn, 0, SEEK_END);
    > fSize = ftell(fIn);
    >
    > Which worked great until now. We have now come across a situation where the
    > file is huge (one specific example is 4,499,692,752 bytes). Even if fSize is
    > declared as "long long", which is big enough to handle that value, it is not
    > geting the correct value. So that tells me that either fseek can't get that
    > far into the file or ftell can't report a value of that size.


    ftell() is defined to return a result of type long. Storing that
    result in a long long object doesn't help.

    > Is there any way I can accomplish this for huge files? This needs to run on
    > both a windows platofrm and linux, which rules out Windows API calls.


    Note that the fseek/ftell trick, though it's likely to work on Windows
    and Linux platforms, is not guaranteed to work in general. If the
    file was opened in text mode, the value returned by ftell isn't
    necessarily meaningful except as an argument to fseek. If the file
    was opened in binary mode, it's permitted to be implicitly padded with
    null bytes at the end.

    There's also the question of just what "the size of a file" means.
    The only really portable way to determine it would be to read the
    entire file and count the bytes; of course that's horribly inefficient
    for large files. And what if the size changes after you determine it
    but before you use it?

    Both Linux and Windows provide efficient system-specific methods to
    determine the current size of a file. You might consider writing both
    versions and using #ifdef. It's probably best to isolate such
    system-specific code.

    > On a similar note - if I wanted to dump the value of a long long or long
    > double to the console using printf, what do I use as the format character?


    For long long, you can use "%lld". For long double, any of "%Lg",
    "%Lf", or "%Le", depending on how you want the output to look.
    Sprinkle to taste with flags and/or length modifiers.

    Any decent C reference should tell you this.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 1, 2009
    #1
    1. Advertising

  2. Keith Thompson

    Flash Gordon Guest

    Keith Thompson wrote:
    > "CapCity" <> writes:
    >> Not a terrbily accomplished C coder here. I have a small ANSI C app that at
    >> some point needs to know the number of bytes in a file. I've seen a few
    >> suggestions along the lines of:
    >>
    >> fseek(fIn, 0, SEEK_END);
    >> fSize = ftell(fIn);
    >>
    >> Which worked great until now. We have now come across a situation where the
    >> file is huge (one specific example is 4,499,692,752 bytes). Even if fSize is


    <snip>

    > Both Linux and Windows provide efficient system-specific methods to
    > determine the current size of a file. You might consider writing both
    > versions and using #ifdef. It's probably best to isolate such
    > system-specific code.


    I agree with Keith that this is probably the best solution.

    It unfortuantely means you might need to ask on both
    comp.unix.programmer and a Windows group for help.

    >> On a similar note - if I wanted to dump the value of a long long or long
    >> double to the console using printf, what do I use as the format character?

    >
    > For long long, you can use "%lld". For long double, any of "%Lg",
    > "%Lf", or "%Le", depending on how you want the output to look.
    > Sprinkle to taste with flags and/or length modifiers.


    Wasn't long long added in C99? If so it won't be available in MS Visual
    Studio (which provides another type to achieve a similar purpose). Also
    if using a compiler that uses the MS C library (rather than its own)
    then even if it supports long long the library won't support the C99
    format specifies for it.

    > Any decent C reference should tell you this.


    I'm sure that none of the editions of K&R will tell you, and K&R2 is
    still a decent reference! However, the man pages on the Linux box and
    the help within the Windows development environment should have provided
    this information.
    --
    Flash Gordon
    Flash Gordon, Oct 2, 2009
    #2
    1. Advertising

  3. Flash Gordon <> writes:
    > Keith Thompson wrote:
    >> "CapCity" <> writes:

    [...]
    >>> On a similar note - if I wanted to dump the value of a long long or
    >>> long double to the console using printf, what do I use as the
    >>> format character?

    >>
    >> For long long, you can use "%lld". For long double, any of "%Lg",
    >> "%Lf", or "%Le", depending on how you want the output to look.
    >> Sprinkle to taste with flags and/or length modifiers.

    >
    > Wasn't long long added in C99? If so it won't be available in MS
    > Visual Studio (which provides another type to achieve a similar
    > purpose). Also if using a compiler that uses the MS C library (rather
    > than its own) then even if it supports long long the library won't
    > support the C99 format specifies for it.


    Quite right. It's still entirely possible to have an implementation
    in which the compiler supports long long but the library doesn't, or
    vice versa.

    >> Any decent C reference should tell you this.

    >
    > I'm sure that none of the editions of K&R will tell you, and K&R2 is
    > still a decent reference! However, the man pages on the Linux box and
    > the help within the Windows development environment should have
    > provided this information.


    Good point. (But long double goes back to C89/C90.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 2, 2009
    #3
  4. Keith Thompson

    Flash Gordon Guest

    Keith Thompson wrote:
    > Flash Gordon <> writes:
    >> Keith Thompson wrote:


    <snip>

    >>> Any decent C reference should tell you this.

    >> I'm sure that none of the editions of K&R will tell you, and K&R2 is
    >> still a decent reference! However, the man pages on the Linux box and
    >> the help within the Windows development environment should have
    >> provided this information.

    >
    > Good point. (But long double goes back to C89/C90.)


    I missed the switch from long long to long double.
    --
    Flash Gordon
    Flash Gordon, Oct 2, 2009
    #4
  5. Keith Thompson

    Phil Carmody Guest

    Flash Gordon <> writes:
    >> (But long double goes back to C89/C90.)

    >
    > I missed the switch from long long to long double.


    ?

    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
    Phil Carmody, Oct 3, 2009
    #5
  6. Keith Thompson

    Flash Gordon Guest

    Phil Carmody wrote:
    > Flash Gordon <> writes:
    >>> (But long double goes back to C89/C90.)

    >> I missed the switch from long long to long double.

    >
    > ?


    Earlier in the post the OP was talking about long long, but in that
    particular bit the OP was talking about long double. The two things were
    unrelated, but my mind failed to make the switch. Either that or I
    miss-read even more than I thought.
    --
    Flash Gordon
    Flash Gordon, Oct 3, 2009
    #6
  7. Keith Thompson

    Phil Carmody Guest

    Flash Gordon <> writes:
    > Phil Carmody wrote:
    >> Flash Gordon <> writes:
    >>>> (But long double goes back to C89/C90.)
    >>> I missed the switch from long long to long double.

    >>
    >> ?

    >
    > Earlier in the post the OP was talking about long long, but in that
    > particular bit the OP was talking about long double. The two things
    > were unrelated, but my mind failed to make the switch. Either that or
    > I miss-read even more than I thought.


    OK, a bit of a thread-local in-joke which wooooshed me completely.

    (Killfile too full, and getting steadily fuller, I probably never
    read the post to which you're refering.)

    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
    Phil Carmody, Oct 3, 2009
    #7
    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. Replies:
    2
    Views:
    340
    Larry I Smith
    Jun 21, 2005
  2. Shaguf
    Replies:
    0
    Views:
    349
    Shaguf
    Dec 24, 2008
  3. Shaguf
    Replies:
    0
    Views:
    447
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    234
    Shaguf
    Dec 26, 2008
  5. Shaguf
    Replies:
    0
    Views:
    213
    Shaguf
    Dec 24, 2008
Loading...

Share This Page