struct stat st; stat(fileName.c_str(), &st); hu?

Discussion in 'C++' started by Steven T. Hatton, Jun 1, 2005.

  1. I found the following two statements in the file linked below:

    struct stat st;
    stat(fileName.c_str(), &st);

    http://websvn.kde.org/branches/work/kdevelop4-parser/main.cpp?rev=420247&view=markup

    This is from /usr/include/sys/stat.h which I'm pretty sure is what #include
    <sys/stat.h> is pulling in. I plain and simply do not understand what the
    above two lines of code do. Can someone please explain?

    extern int stat (__const char *__restrict __file,
    struct stat *__restrict __buf) __THROW;

    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
     
    Steven T. Hatton, Jun 1, 2005
    #1
    1. Advertising

  2. Steven T. Hatton

    Pete Becker Guest

    Steven T. Hatton wrote:
    > I found the following two statements in the file linked below:
    >
    > struct stat st;
    > stat(fileName.c_str(), &st);
    >
    > http://websvn.kde.org/branches/work/kdevelop4-parser/main.cpp?rev=420247&view=markup
    >
    > This is from /usr/include/sys/stat.h which I'm pretty sure is what #include
    > <sys/stat.h> is pulling in. I plain and simply do not understand what the
    > above two lines of code do. Can someone please explain?
    >
    > extern int stat (__const char *__restrict __file,
    > struct stat *__restrict __buf) __THROW;
    >


    Pretend it says this:

    struct stat
    {
    // whatever...
    };

    extern int STAT(const char *, struct stat *);

    struct stat st; // defines object of type struct stat
    STAT(fileName.c_str(), &st); // call STAT

    This is a quirk in the standard C library: there's a struct named 'stat'
    and a function named 'stat'. When you say "struct stat" you're talking
    about the struct; when you say "stat" you're talking about the function.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Jun 1, 2005
    #2
    1. Advertising

  3. Steven T. Hatton

    Jack Klein Guest

    On Wed, 01 Jun 2005 11:44:44 -0400, Pete Becker <>
    wrote in comp.lang.c++:

    > Pretend it says this:
    >
    > struct stat
    > {
    > // whatever...
    > };
    >
    > extern int STAT(const char *, struct stat *);
    >
    > struct stat st; // defines object of type struct stat
    > STAT(fileName.c_str(), &st); // call STAT
    >
    > This is a quirk in the standard C library: there's a struct named 'stat'
    > and a function named 'stat'. When you say "struct stat" you're talking
    > about the struct; when you say "stat" you're talking about the function.


    If it was anyone who didn't work for Dinkumware, I'd have to point
    that the problem is not with the "standard C" library.

    Oh, what the heck, I just did that anyway.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jun 2, 2005
    #3
  4. Steven T. Hatton

    Pete Becker Guest

    Jack Klein wrote:
    > On Wed, 01 Jun 2005 11:44:44 -0400, Pete Becker <>
    > wrote in comp.lang.c++:
    >
    >
    >>Pretend it says this:
    >>
    >>struct stat
    >>{
    >>// whatever...
    >>};
    >>
    >>extern int STAT(const char *, struct stat *);
    >>
    >>struct stat st; // defines object of type struct stat
    >>STAT(fileName.c_str(), &st); // call STAT
    >>
    >>This is a quirk in the standard C library: there's a struct named 'stat'
    >>and a function named 'stat'. When you say "struct stat" you're talking
    >>about the struct; when you say "stat" you're talking about the function.

    >
    >
    > If it was anyone who didn't work for Dinkumware, I'd have to point
    > that the problem is not with the "standard C" library.
    >
    > Oh, what the heck, I just did that anyway.
    >


    Good point. It's not the standard C library; it's the usual C library,
    i.e., a UNIXism incorporated into POSIX and into most C libraries. I
    don't know of any other code that does this, and it required a change to
    the original grammar in C++ to make it work.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Jun 2, 2005
    #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. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  2. Patrick Useldinger

    os.stat('<filename>')[stat.ST_INO] on Windows

    Patrick Useldinger, Feb 27, 2005, in forum: Python
    Replies:
    6
    Views:
    1,178
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 3, 2005
  3. Magesh
    Replies:
    3
    Views:
    444
    Gordon Burditt
    Oct 5, 2007
  4. Rolf Krüger
    Replies:
    2
    Views:
    616
    Ian Collins
    Mar 12, 2008
  5. ruck
    Replies:
    10
    Views:
    1,278
Loading...

Share This Page