Reading The File Status


The important steps in Reading the file status

Open the File:
Use the fopen() function to open the file in read mode.
The file path and mode (e.g., "r") are required parameters.

Read File Properties:
Use the stat() function to retrieve information about the file.
The stat structure contains details such as file size, permissions, creation time, and modification time.

Print File Properties:
Extract relevant information from the stat structure.
Print details like file size, creation date, and modification date. 

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>

int main() {
char filename[50];
struct stat fileStat;

printf("Enter the filename:");
scanf("%s",filename);

// Call stat() to get file status

if (stat(filename, &fileStat) == -1) {
perror("Error");
return 1;
}

printf("Information for %s\n",filename);
printf("---------------------------\n");
printf("File Size: \t\t%ld bytes\n", fileStat.st_size);
printf("Number of Links: \t%ld\n", fileStat.st_nlink);
printf("File inode: \t\t%ld\n", fileStat.st_ino);

printf("File Permissions: \t");
printf((S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf((fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf((fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf((fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf((fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf((fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf((fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf((fileStat.st_mode & S_IROTH) ? "r" : "-");
printf((fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf((fileStat.st_mode & S_IXOTH) ? "x\n" : "-\n");

printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
printf("Last access time: \t%s", ctime(&fileStat.st_atime)); 
printf("Last modification time: \t%s", ctime(&fileStat.st_mtime)); 
printf("Last status change time: \t%s", ctime(&fileStat.st_ctime));

return 0;
}

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

void printFileProperties(struct stat stats) {
    struct tm dt;

    // File permissions
    printf("\nFile access: ");
    if (stats.st_mode & S_IRUSR) printf("read ");
    if (stats.st_mode & S_IWUSR) printf("write ");
    if (stats.st_mode & S_IXUSR) printf("execute");

    // File size
    printf("\nFile size: %ld bytes", stats.st_size);

    // Creation time
    dt = *(gmtime(&stats.st_ctime));
    printf("\nCreated on: %d-%d-%d %d:%d:%d",
           dt.tm_mday, dt.tm_mon + 1, dt.tm_year + 1900,
           dt.tm_hour, dt.tm_min, dt.tm_sec);

    // Modification time
    dt = *(gmtime(&stats.st_mtime));
    printf("\nModified on: %d-%d-%d %d:%d:%d",
           dt.tm_mday, dt.tm_mon + 1, dt.tm_year + 1900,
           dt.tm_hour, dt.tm_min, dt.tm_sec);
}

int main() {
    char fname[100];
    struct stat stats;

    printf("Enter the file name: ");
    scanf("%s", fname);

    if (stat(fname, &stats) == 0) {
        printFileProperties(stats);
    } else {
        printf("Unable to get file properties.\n");
        printf("Please check whether '%s' file exists.\n", fname);
    }

    return 0;
}

Comments

Popular posts from this blog

CSL 204 OPERATING SYSTEM LAB KTU IV SEM CSE

FCFS and SJF

Shared Memory Inter-Process Communication ( IPC)