7 Replies - 1024 Views - Last Post: 04 March 2010 - 02:16 PM Rate Topic: -----

#1 jbedo465  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 10

stat() and information about file in C

Posted 04 March 2010 - 11:25 AM

Hi all,

I need to find out all of the information about a file entered as a command line argument.
I'm using stat() to find out the simpler information such as file size and date last accessed. However, I need to find other info, such as the username of the file owner, not just their uid, as well as their groups, not just there gid. I can find the uid and gid fine with stat(). No idea how to find this stuff. Searched online but there was surprisingly little stuff about stat. Also, with the dates accessed, it gives it in seconds since 1970. How would I convert that to a date? Here is what I have so far:

#include <string.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <locale.h>
#include <langinfo.h>
#include <stdint.h>

int main(int argc, char *argv[]){
  FILE *fp;
  struct stat fileAtt;

    if(stat(argv[1], &fileAtt) == 0){
      printf("File Name: %s\n", argv[1]);
      printf("UID : %d\nGID : %d\n", fileAtt.st_uid, fileAtt.st_gid);
      printf("File size is: %lu\n", fileAtt.st_size);
      printf("Last accessed: %u\n", fileAtt.st_atime);
    }

return 0;
}




Thanks in advance,
JB

Is This A Good Question/Topic? 0
  • +

Replies To: stat() and information about file in C

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: stat() and information about file in C

Posted 04 March 2010 - 11:49 AM

I would use the GetFileInformationByHandle Windows API call. Details may be found here.
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: stat() and information about file in C

Posted 04 March 2010 - 11:50 AM

He uses UNIX based system. Look at the header files his program uses.

This post has been edited by sarmanu: 04 March 2010 - 11:50 AM

Was This Post Helpful? 1
  • +
  • -

#4 jbedo465  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 10

Re: stat() and information about file in C

Posted 04 March 2010 - 12:21 PM

Yea, I'm using UNIX.
Was This Post Helpful? 0
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: stat() and information about file in C

Posted 04 March 2010 - 12:21 PM

Thanks sarmanu, I have now put my glasses on and can see much better. :blink:
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5958
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: stat() and information about file in C

Posted 04 March 2010 - 01:09 PM

For time functions, Google man 3 time, or type man 3 time into the Unix shell.
Was This Post Helpful? 1
  • +
  • -

#7 jbedo465  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 10

Re: stat() and information about file in C

Posted 04 March 2010 - 01:57 PM

Thanks for the help with the dates. Got that working, any idea for the other information? Still need to find Username, I can only get UID.

Thanks,
JB
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5958
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: stat() and information about file in C

Posted 04 March 2010 - 02:16 PM

man getpwuid
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1