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

  • Thread starter Steven T. Hatton
  • Start date
S

Steven T. Hatton

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;
 
P

Pete Becker

Steven said:
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.
 
J

Jack Klein

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

Pete Becker

Jack said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top