WIN10DBubuntu
WIN10DBubuntu

Reputation: 1

[C][Stat][Fileinfo] Why is st_mode defined as something not in structure, when I use the stat() call to return a structure?

I'm trying to use the st_mode from a returned stat struct, that I get from the stat() call following way;

        char *fn = "test.c"

        struct stat *buf = malloc(sizeof(stat));

        stat(fn,buf);

        if(S_ISREG(buf.st_mode))
        {
          printf("this file is a regular file..."); //if regular
        }

When I try to compile this code, I get the following error:

server1.c: In function ‘main’:
server1.c:223:32: error: request for member ‘st_mode’ in something not 
a structure or union
         if(S_ISREG(fileData.st_mode))
                            ^

Why do I get this compile error? I can't seem to find much info myself..

Upvotes: 0

Views: 926

Answers (1)

buf is not a struct stat. buf is a pointer to a struct stat, and pointers don't have st_mode fields. To get the st_mode field of the struct stat that buf points to, use (*buf).st_mode, or buf->st_mode for short.

Upvotes: 1

Related Questions