c program which accept one argument as a directory name and
prints all the file name along with its inode number and
total count of the file in directory

Answer Posted / rakesh

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stddef.h>
#include <sys/stat.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
DIR *dip;
struct dirent *dit;
struct stat sb;
int i = 0;
if(argc < 2)
{
printf("Usage: %s <directory>\n", argv[0]);
return 0;
}
if((dip = opendir(argv[1])) == NULL)
{
perror("opendir");
return 0;
}
printf("Directory stream is now open\n");
while ((dit = readdir(dip)) != NULL)
{
i++;
stat(dit->d_name,&sb);
printf("%u \t%s\n",sb.st_ino,dit->d_name);
}
printf("No. of Files in directory are: %i \n", i);
if(closedir(dip)== -1)
{
perror("closedir");
return 0;
}
printf("\nDirectory stream is now closed\n");
return 1;
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write a command sequence to find all the files modified in less than 2 days and print the record count of each.

941


Which scripting language is best for automation?

541


how to get part of string variable with echo command only?

605


Write a shell script that adds two numbers if provided as the command line argument and if the two numbers are not entered throws an error message.

810


Why are there shells on the beach?

607






What does $@ mean bash?

672


What is meant by $1 in shell script?

558


How do scripts work?

618


Is shell and terminal the same?

602


How can you find out how long the system has been running?

530


How do you find out What is your shell?

595


What will happen to my current process when I execute a command using exec?

545


What is shell scripting used for?

581


What's the difference between scripting and coding?

612


What is wc in shell script?

561