Help ! how to get a file size larger than 2G?

Discussion in 'C Programming' started by zou, Sep 30, 2007.

  1. zou

    zou Guest

    there is a file which is very large,

    we can use stat to get a file size(<2G),
    struct stat buf;
    stat("file", &buf);
    long s=(long)stat.st_size;

    but stat::st_size is type of off_t(typedef long),
    so how about a file larger than 2G?

    many thanks~
    zou, Sep 30, 2007
    #1
    1. Advertising

  2. zou

    santosh Guest

    zou wrote:

    > there is a file which is very large,
    >
    > we can use stat to get a file size(<2G),
    > struct stat buf;
    > stat("file", &buf);
    > long s=(long)stat.st_size;
    >
    > but stat::st_size is type of off_t(typedef long),
    > so how about a file larger than 2G?


    You'll have to investigate the options provided by your platform. Standard C
    itself specifies no functions to get the size of a "file." The only fully
    portable method is to read through the file and keep count, and even then,
    it's size "on disk" might be different to the size calculated by reading
    it.

    You also seem to be using C++. If so, then the appropriate group is
    comp.lang.c++.
    santosh, Sep 30, 2007
    #2
    1. Advertising

  3. zou

    Ian Collins Guest

    zou wrote:
    > there is a file which is very large,
    >
    > we can use stat to get a file size(<2G),
    > struct stat buf;
    > stat("file", &buf);
    > long s=(long)stat.st_size;
    >
    > but stat::st_size is type of off_t(typedef long),
    > so how about a file larger than 2G?
    >

    You are as off topic here as you were in c.l.c++, try comp.unix.programmer.

    --
    Ian Collins.
    Ian Collins, Sep 30, 2007
    #3
  4. zou

    CBFalconer Guest

    zou wrote:
    >
    > there is a file which is very large,
    >
    > we can use stat to get a file size(<2G),
    > struct stat buf;
    > stat("file", &buf);
    > long s=(long)stat.st_size;
    >
    > but stat::st_size is type of off_t(typedef long),
    > so how about a file larger than 2G?


    stat and off_t are not part of standard C and thus off-topic here.
    Find a newsgroup dealing with your system, possibly
    comp.unix.programmer.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 30, 2007
    #4
  5. On Sep 30, 1:06 pm, CBFalconer <> wrote:
    > zou wrote:
    >
    > > there is a file which is very large,

    >
    > > we can use stat to get a file size(<2G),
    > > struct stat buf;
    > > stat("file", &buf);
    > > long s=(long)stat.st_size;

    >
    > > but stat::st_size is type of off_t(typedef long),
    > > so how about a file larger than 2G?

    >
    > stat and off_t are not part of standard C and thus off-topic here.
    > Find a newsgroup dealing with your system, possibly
    > comp.unix.programmer.
    >


    It looks like this function is only available to Unix systems.
    I think, you can check with the man page of stat.

    C version of code ;-)
    Refer this link - http://www.phim.unibe.ch/comp_doc/c_manual/C/EXAMPLES/stat.c

    #include <sys/stat.h> /* declare the 'stat' structure */

    struct stat stat_p; /* 'stat_p' is a pointer to a structure
    * of type 'stat'. */

    if ( -1 == stat (filename, &stat_p))
    {
    printf(" Error occoured attempting to stat %s\n", filename);
    exit(0);
    }

    printf("File size is %d bytes\n", stat_p.st_size);

    Karthik Balaguru
    karthikbalaguru, Sep 30, 2007
    #5
  6. "karthikbalaguru" <> a écrit dans le message de
    news: ...
    > On Sep 30, 1:06 pm, CBFalconer <> wrote:
    >> zou wrote:
    >>
    >> > there is a file which is very large,

    >>
    >> > we can use stat to get a file size(<2G),
    >> > struct stat buf;
    >> > stat("file", &buf);
    >> > long s=(long)stat.st_size;

    >>
    >> > but stat::st_size is type of off_t(typedef long),
    >> > so how about a file larger than 2G?

    >>
    >> stat and off_t are not part of standard C and thus off-topic here.
    >> Find a newsgroup dealing with your system, possibly
    >> comp.unix.programmer.
    >>

    >
    > It looks like this function is only available to Unix systems.
    > I think, you can check with the man page of stat.


    Wrong, it is available on a lot of non-Unix systems: cygwin, Windows (albeit
    renamed to _stat)

    > C version of code ;-)
    > Refer this link -
    > http://www.phim.unibe.ch/comp_doc/c_manual/C/EXAMPLES/stat.c


    That code looks pretty old and bogus

    >
    > #include <sys/stat.h> /* declare the 'stat' structure */


    These are possibly also needed:
    #include <sys/types.h>
    #include <unistd.h>

    >
    > struct stat stat_p; /* 'stat_p' is a pointer to a structure
    > * of type 'stat'. */


    wrong, stat_p is a struct with an utterly misleading name


    > if ( -1 == stat (filename, &stat_p))


    This style looks childish to me.

    > {
    > printf(" Error occoured attempting to stat %s\n", filename);


    More typos, plus errors should go to stderr.

    > exit(0);


    Better exit with EXIT_FAILURE if this is an error

    > }
    >
    > printf("File size is %d bytes\n", stat_p.st_size);


    stat_p.st_size is not an int.
    It is an off_t: possibly a long or a long long.
    It would be safer to write:

    printf("File size is %lld bytes\n", (long long)stat_p.st_size);

    Your post was off topic, and a correct answer had already been given with an
    appropriate newsgroup to query.
    To make things worse, your anwser is misleading and erroneous.
    Please refrain from posting answers on topics you do not master, especially
    when the question is off topic already.

    --
    Chqrlie.
    Charlie Gordon, Sep 30, 2007
    #6
  7. zou

    Guest

    On Sep 30, 9:21 am, zou <> wrote:
    > there is a file which is very large,
    >
    > we can use stat to get a file size(<2G),
    > struct stat buf;
    > stat("file", &buf);
    > long s=(long)stat.st_size;
    >
    > but stat::st_size is type of off_t(typedef long),
    > so how about a file larger than 2G?
    >
    > many thanks~


    What kind of compilers are you using?

    I once did so in Microsoft Visual C++ 6.0. According to GNU C library,
    there should be some functions like stat64/fstat64, but I cannot get
    it work in Cygwin. I guess that stat64 would work in Linux/Unix.

    Regards,
    Tian
    , Sep 30, 2007
    #7
  8. zou

    santosh Guest

    karthikbalaguru wrote:

    > On Sep 30, 1:06 pm, CBFalconer <> wrote:
    >> zou wrote:
    >>
    >> > there is a file which is very large,

    >>
    >> > we can use stat to get a file size(<2G),
    >> > struct stat buf;
    >> > stat("file", &buf);
    >> > long s=(long)stat.st_size;

    >>
    >> > but stat::st_size is type of off_t(typedef long),
    >> > so how about a file larger than 2G?

    >>
    >> stat and off_t are not part of standard C and thus off-topic here.
    >> Find a newsgroup dealing with your system, possibly
    >> comp.unix.programmer.
    >>

    >
    > It looks like this function is only available to Unix systems.
    > I think, you can check with the man page of stat.
    >
    > C version of code ;-)
    > Refer this link -
    > http://www.phim.unibe.ch/comp_doc/c_manual/C/EXAMPLES/stat.c
    >
    > #include <sys/stat.h> /* declare the 'stat' structure */
    >
    > struct stat stat_p; /* 'stat_p' is a pointer to a structure
    > * of type 'stat'. */
    >
    > if ( -1 == stat (filename, &stat_p))
    > {
    > printf(" Error occoured attempting to stat %s\n", filename);
    > exit(0);
    > }
    >
    > printf("File size is %d bytes\n", stat_p.st_size);


    Please attempt, wherever possible, to provide correct and compilable code.
    The above code fragment, besides being uncompilable, is also not correct,
    in that it invokes undefined behaviour in at least one place.
    santosh, Sep 30, 2007
    #8
  9. In article <fdmtlu$kd$>, zou <> wrote:

    > but stat::st_size is type of off_t(typedef long),
    > so how about a file larger than 2G?


    I suppose by "stat::st_size" you mean the st_size field of the stat
    structure. Note that stat() isn't a standard C function, but a unix
    one which has made its way into some other systems.

    Some systems have a symbol you can #define to make off_t be a 64-bit
    type on a system where it is usually 32 bits. It appears to be
    __USE_FILE_OFFSET64 on a Linux system here, but I have no experience
    of using it so I recommend you ask in a group more specific to your
    system.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Sep 30, 2007
    #9
  10. On Sun, 30 Sep 2007 17:51:08 +0530, santosh
    <> wrote:

    >karthikbalaguru wrote:
    >
    >> On Sep 30, 1:06 pm, CBFalconer <> wrote:
    >>> zou wrote:
    >>>
    >>> > there is a file which is very large,
    >>>
    >>> > we can use stat to get a file size(<2G),
    >>> > struct stat buf;
    >>> > stat("file", &buf);
    >>> > long s=(long)stat.st_size;
    >>>
    >>> > but stat::st_size is type of off_t(typedef long),
    >>> > so how about a file larger than 2G?
    >>>
    >>> stat and off_t are not part of standard C and thus off-topic here.
    >>> Find a newsgroup dealing with your system, possibly
    >>> comp.unix.programmer.
    >>>

    >>
    >> It looks like this function is only available to Unix systems.
    >> I think, you can check with the man page of stat.
    >>
    >> C version of code ;-)
    >> Refer this link -
    >> http://www.phim.unibe.ch/comp_doc/c_manual/C/EXAMPLES/stat.c
    >>
    >> #include <sys/stat.h> /* declare the 'stat' structure */
    >>
    >> struct stat stat_p; /* 'stat_p' is a pointer to a structure
    >> * of type 'stat'. */
    >>
    >> if ( -1 == stat (filename, &stat_p))
    >> {
    >> printf(" Error occoured attempting to stat %s\n", filename);
    >> exit(0);
    >> }
    >>
    >> printf("File size is %d bytes\n", stat_p.st_size);

    >
    >Please attempt, wherever possible, to provide correct and compilable code.
    >The above code fragment, besides being uncompilable, is also not correct,
    >in that it invokes undefined behaviour in at least one place.
    >


    You've misread the posting.


    Richard Harter,
    http://home.tiac.net/~cri, http://www.varinoma.com
    But the rhetoric of holistic harmony can generate into a kind of
    dotty, Prince Charles-style mysticism. -- Richard Dawkins
    Richard Harter, Sep 30, 2007
    #10
  11. zou

    Richard Guest

    santosh <> writes:

    > karthikbalaguru wrote:
    >
    >> On Sep 30, 1:06 pm, CBFalconer <> wrote:
    >>> zou wrote:
    >>>
    >>> > there is a file which is very large,
    >>>
    >>> > we can use stat to get a file size(<2G),
    >>> > struct stat buf;
    >>> > stat("file", &buf);
    >>> > long s=(long)stat.st_size;
    >>>
    >>> > but stat::st_size is type of off_t(typedef long),
    >>> > so how about a file larger than 2G?
    >>>
    >>> stat and off_t are not part of standard C and thus off-topic here.
    >>> Find a newsgroup dealing with your system, possibly
    >>> comp.unix.programmer.
    >>>

    >>
    >> It looks like this function is only available to Unix systems.
    >> I think, you can check with the man page of stat.
    >>
    >> C version of code ;-)
    >> Refer this link -
    >> http://www.phim.unibe.ch/comp_doc/c_manual/C/EXAMPLES/stat.c
    >>
    >> #include <sys/stat.h> /* declare the 'stat' structure */
    >>
    >> struct stat stat_p; /* 'stat_p' is a pointer to a structure
    >> * of type 'stat'. */
    >>
    >> if ( -1 == stat (filename, &stat_p))
    >> {
    >> printf(" Error occoured attempting to stat %s\n", filename);
    >> exit(0);
    >> }
    >>
    >> printf("File size is %d bytes\n", stat_p.st_size);

    >
    > Please attempt, wherever possible, to provide correct and compilable code.
    > The above code fragment, besides being uncompilable, is also not correct,
    > in that it invokes undefined behaviour in at least one place.


    Since the undefined behaviour is nothing to so with stat, as such,
    possibly you could explain? Surely not all "samples" that people post
    where they are having difficulties need to be sanitised?!?!? At first
    glance all I see really wrong is the comment about stat_p being a pointer. It
    isn't. And of course the lack of an include for printf.
    Richard, Sep 30, 2007
    #11
  12. "Richard" <> schrieb im Newsbeitrag
    news:...
    > santosh <> writes:
    >
    >> karthikbalaguru wrote:
    >>
    >>> On Sep 30, 1:06 pm, CBFalconer <> wrote:
    >>>> zou wrote:
    >>>>
    >>>> > there is a file which is very large,
    >>>>
    >>>> > we can use stat to get a file size(<2G),
    >>>> > struct stat buf;
    >>>> > stat("file", &buf);
    >>>> > long s=(long)stat.st_size;
    >>>>
    >>>> > but stat::st_size is type of off_t(typedef long),
    >>>> > so how about a file larger than 2G?
    >>>>
    >>>> stat and off_t are not part of standard C and thus off-topic here.
    >>>> Find a newsgroup dealing with your system, possibly
    >>>> comp.unix.programmer.
    >>>>
    >>>
    >>> It looks like this function is only available to Unix systems.
    >>> I think, you can check with the man page of stat.
    >>>
    >>> C version of code ;-)
    >>> Refer this link -
    >>> http://www.phim.unibe.ch/comp_doc/c_manual/C/EXAMPLES/stat.c
    >>>
    >>> #include <sys/stat.h> /* declare the 'stat' structure */
    >>>
    >>> struct stat stat_p; /* 'stat_p' is a pointer to a structure
    >>> * of type 'stat'. */
    >>>
    >>> if ( -1 == stat (filename, &stat_p))
    >>> {
    >>> printf(" Error occoured attempting to stat %s\n", filename);
    >>> exit(0);
    >>> }
    >>>
    >>> printf("File size is %d bytes\n", stat_p.st_size);

    >>
    >> Please attempt, wherever possible, to provide correct and compilable
    >> code.
    >> The above code fragment, besides being uncompilable, is also not correct,
    >> in that it invokes undefined behaviour in at least one place.

    >
    > Since the undefined behaviour is nothing to so with stat, as such,
    > possibly you could explain? Surely not all "samples" that people post
    > where they are having difficulties need to be sanitised?!?!? At first
    > glance all I see really wrong is the comment about stat_p being a pointer.
    > It
    > isn't. And of course the lack of an include for printf.

    And exactly that missing "#include <stdio.h>" is causing undefined behaviour
    (varadic function without prototype).

    Bye, Jojo
    Joachim Schmitz, Sep 30, 2007
    #12
  13. zou

    Richard Guest

    "Joachim Schmitz" <> writes:
    >> Since the undefined behaviour is nothing to so with stat, as such,
    >> possibly you could explain? Surely not all "samples" that people post
    >> where they are having difficulties need to be sanitised?!?!? At first
    >> glance all I see really wrong is the comment about stat_p being a pointer.
    >> It
    >> isn't. And of course the lack of an include for printf.

    > And exactly that missing "#include <stdio.h>" is causing undefined behaviour
    > (varadic function without prototype).
    >
    > Bye, Jojo


    I know. hence I pointed it out. The point here is that yet again we have
    "noise" shouting "OT" while not actually providing any help.
    Richard, Sep 30, 2007
    #13
    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. Peter Williams
    Replies:
    1
    Views:
    1,354
    Dylan Parry
    Jun 3, 2005
  2. Wes
    Replies:
    6
    Views:
    493
    Old Wolf
    Oct 24, 2006
  3. zou
    Replies:
    3
    Views:
    560
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Sep 30, 2007
  4. Replies:
    5
    Views:
    489
    Colin Song
    Oct 4, 2007
  5. Bakes

    FTP Offset larger than file.

    Bakes, Jul 28, 2009, in forum: Python
    Replies:
    6
    Views:
    772
    Bakes
    Jul 28, 2009
Loading...

Share This Page