C++ program to read and display the i-node information for a given text file, image file. PL1(GAQ4)

/* Write a program in Python/C++ to read display the i-node information for a given text file,
image file.*/
//by Ankit Singhaniya
#include<iostream>
#include<sys/stat.h>
using namespace std;

int main()
{
char fname[255];
cout<<“Enter absolute path for your file: “;                 //eg: /home/student/g1q4.cpp
cin>>fname;
struct stat var;                             //a variable that can store the stat information if the file we provide.
int ret=stat(fname,&var);                        //call the fuction stat for file name ‘fname’ and store the values in ‘var’.

if(ret<0)
{
cout<<“System call ‘stat’ exited with an error code “<<ret<<endl;
}
else
{
cout<<“Device id: “<<var.st_dev<<endl;
cout<<“Inode number: “<<var.st_ino<<endl;
cout<<“Mode: “<<var.st_mode<<endl;
cout<<“UID: “<<var.st_uid<<endl;
cout<<“GID: “<<var.st_gid<<endl;
cout<<“Size: “<<var.st_size<<endl;
}

//you can get this structure by $man fstat (trick, not to be used in exams)
struct stat
{
dev_t     st_dev;     /* ID of device containing file */
ino_t     st_ino;     /* inode number */
mode_t    st_mode;    /* protection */
nlink_t   st_nlink;   /* number of hard links */
uid_t     st_uid;     /* user ID of owner */
gid_t     st_gid;     /* group ID of owner */
off_t     st_size;    /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
};

}

 

______________________________________________________________________

output samples:

singhaniya@singhaniya-SVF14A15SNB:~/pl1$ g++ g1q4.cpp
singhaniya@singhaniya-SVF14A15SNB:~/pl1$ ./a.out
Enter absolute path for your file: /home/singhaniya/pl1/g1q4.cpp
Device id: 2054
Inode number: 791790
Mode: 33204
UID: 1000
GID: 1000
Size: 1532
singhaniya@singhaniya-SVF14A15SNB:~/pl1$ ./a.out
Enter absolute path for your file: /home/dfsd
System call ‘stat’ exited with an error code -1
singhaniya@singhaniya-SVF14A15SNB:~/pl1$