Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why do I have to prefix stat from <sys/stat.h> with the keyword struct?

Reply
Thread Tools

Why do I have to prefix stat from <sys/stat.h> with the keyword struct?

 
 
Rolf Krüger
Guest
Posts: n/a
 
      03-12-2008
Hi


I´m about learning C/C++ and after covering the language basics I´m now
heading for my first "real" application where I need to use the POSIX stuff
for directory operations.

Here´s my problem: The following code compiles and runs as it should inside
a linux g++ environment using code::blocks IDE. But when I drop
the "struct" prefix from line 12 "struct stat st;" It doesn´t compile any
more and it says:

|12|error: expected `;' before ‘st’
|12|warning: statement is a reference, not call, to function ‘stat’
|12|warning: statement has no effect
|18|error: ‘st’ was not declared in this scope

Hmm, what´s going on here? Why can I drop "struct" from line 1? IMHO dirent
is just a struct as stat is. A similar behaviour can be found using the
cygwin environment under windows, so I guess it has nothing to do with
system dependent header files.


Any ideas?
Thanx in advance
Rolf



#include <iostream>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

using namespace std;

int main() {

dirent *entry;
struct stat st;

DIR *base;
base = opendir(".");

while( (entry = readdir(base)) != 0) {
stat(entry->d_name, &st);
cout << entry->d_name << " :: " << st.st_mode << endl;
}

closedir(base);

return 0;
}

 
Reply With Quote
 
 
 
 
Rolf Krüger
Guest
Posts: n/a
 
      03-12-2008
Hi Ian,

>> Here´s my problem: The following code compiles and runs as it should
>> inside a linux g++ environment using code::blocks IDE. But when I drop
>> the "struct" prefix from line 12 "struct stat st;" It doesn´t compile any
>> more and it says:
>>
>> |12|error: expected `;' before ‘st’
>> |12|warning: statement is a reference, not call, to function ‘stat’
>> |12|warning: statement has no effect
>> |18|error: ‘st’ was not declared in this scope


> stat is both a struct and a function. Very confusing, but simple to
> disambiguate with 'struct stat'.


Thank you very much

I´ve taken more than one close look into the sys/stat.h file ... obviously
not enough - should have seen this by myself!

Thanks
Rolf



 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-12-2008
Rolf Krüger wrote:
> Hi
>
>
> I´m about learning C/C++ and after covering the language basics I´m now
> heading for my first "real" application where I need to use the POSIX stuff
> for directory operations.
>
> Here´s my problem: The following code compiles and runs as it should inside
> a linux g++ environment using code::blocks IDE. But when I drop
> the "struct" prefix from line 12 "struct stat st;" It doesn´t compile any
> more and it says:
>
> |12|error: expected `;' before ‘st’
> |12|warning: statement is a reference, not call, to function ‘stat’
> |12|warning: statement has no effect
> |18|error: ‘st’ was not declared in this scope
>

stat is both a struct and a function. Very confusing, but simple to
disambiguate with 'struct stat'.

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.stat() distorts filenames that end with period (nt.stat()) ruck Python 10 09-07-2012 05:54 AM
Date & Time representation in "struct stat" - <sys/stat.h> Magesh C Programming 3 10-05-2007 12:13 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
struct stat st; stat(fileName.c_str(), &st); hu? Steven T. Hatton C++ 3 06-02-2005 01:50 PM
os.stat('<filename>')[stat.ST_INO] on Windows Patrick Useldinger Python 6 03-03-2005 09:21 AM



Advertisments