Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 2: Printing the time that the file path was created (50 points) The stat sy

ID: 3879929 • Letter: T

Question

Task 2: Printing the time that the file path was created (50 points) The stat system call can access a file by name and retrieve file status information. It is given by: int stat (const char pathname, struct stat buf) This system call returns a stat structure (both the function and struct are called stat), which contains the following fields struct stat t st_devID of device containing file ino t st ino;/inode number mode_t st_mode; / protection t st nlink; / number of hard links/ t st uid * user ID of owncr/ gid t st gidgroup ID of owner t st rdev;device ID (if special file) t st size; total size, in bytes t st blksize; blocksize for file system LO blkcnt t st blocks; / number of 512B blocks allocated c t st atime; time of last access time t st mtime; time of last modification/ time t st ctime; time of last status change On success, zero is returned. On error, is retuned, and errno is set appropriately. The following program displays the time that the file path was last accessed. Type in and complete the following program on the computer. Save t as printaccess.c. Compile the program, then execute it by giving it a path name as an argument. rintaccess.c: #i #include include sys/stat.h> int main (int argc, char *argv) nclude f define an instance of struct stat (don't use a pointer). Use the stat function store the status of the file in your stat struct stat takes a pointer, so pass your struct by reference with & if /i use perror to print error message then exit program print the last access time for the file using defined instance of Il struct stat. ctime printf ("%s last accessed at %s", argv[1]. ctime(&_statime)); requires a pointer, hence the & below

Explanation / Answer

#include <stdio.h>
#include <time.h>
#include <sys/stat.h>

int main(int argc, char *argv[]){
//define an instance of struct stat (dont use a pointer)
struct stat stat_instance;
//Use the stat function store the status of the file in your stat struct
//stat takes a pointer, so pass your struct by reference with &

if( stat(argv[1],&stat_instance) == -1){
//use perror to print error message then exit program
perror("Error: ");
return -1;
}
//print the last access time for the file using defined instance of
//struct stat.ctime requires a pointer, hence the & below.
printf("%s last accesssed at %s", argv[1], ctime(& stat_instance.st_atime));
return 0;
}

// compilation step

//gcc -o a printaccess.c

// ./a sample.txt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote