Write a program to print file details including owner access permissions, file access time, where file name is given as argument

by

Last updated on Nov 4, 2022
Operating System Practicals (Question 4)

Input

#include<iostream>
#include<fcntl.h>
#include<sys/stat.h>
using namespace std;
int main(int argc , char *argv[])
{
    char time[50];
    struct stat s;
    int fd;
    if(argc!=2)
    {
        cout<<"Wrong arguments";
        return 0;
    }
    
    fd=open(argv[1], O_RDONLY);
    fstat(fd,&s);
    
    cout<<"Owner ID : "<<s.st_uid<<"\n";
    cout<<"Group ID : "<<s.st_gid<<"\n";
    printf("Mode:%o(octal)\n",s.st_mode & 0777);
    strftime(time,sizeof (time) , "%d.%m.%Y.%H.%M.%S" , localtime(&s.st_atime));
    cout<<"Last access time = "<<time<<"\n";
    strftime(time,sizeof (time) , "%d.%m.%Y.%H.%M.%S" , localtime(&s.st_mtime));
    cout<<"Last modified time = "<<time<<"\n";
    
    return 0;
}

How useful was this post?

5 star mean very useful & 1 star means not useful at all.

Average rating 4.3 / 5. Vote count: 4

No votes so far! Be the first to rate this post.