Question

Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program...

Write a C program countFiles.c to be executed on the command line as follows:

countFiles <directory>

The program should count the (regular) files in the specified directory as well as all subdirectories and output the total number on the console. Files and subdirectories whose names .start with should be ignored!

To do this, define a function int countFilesRec(char* dirName)that dirName returns the number of (regular) files in the directory and all the subdirectories. Call the function recursively to count the files in the subdirectories. Note that you may need to concatenate the directory names appropriately in the recursive call.

Note: Use the C functions for implementation opendir, readdir and closedir

Test: With the following command line call you can determine the number of files in a directory and all subdirectories under Linux for comparison with your program under Linux:

find <directory> -type f | wc -l
0 0
Add a comment Improve this question Transcribed image text
Answer #1

solution:
This solution has a full explanation, full c code, comments with code and screenshot of

execution of the program for your clear and better understanding.

EXPLANATION:

Before going to directly answer first let's see the approach for solving this problem.

First, open dir and find all regular files and then if subdirectory found then recursively find all the subregular files from that sub dir.

and after reading all the dir finally close them.

For more visualization see below:

c code :

// header files
#include<stdio.h>
#include <dirent.h>
#include<string.h>
#include<stdlib.h>
// variables to count files
int fileCount,count;
char *pathP;
// function to count total files
int countFiles(char *path){
   count++;
   // DIR object ptr
   DIR *dirp;
   struct dirent * entry;
   if(count>1){
   // adding path name
   strcat(pathP,"/");
   strcat(pathP,path);
   dirp = opendir(pathP);
   }
   else dirp = opendir(path);
   // for error check
   if(!dirp) {
       printf("\noops! Something goes wrong with system\n");
       exit(1);
   }
   // readdir to read a directory & checking for regular files
   while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_REG) /* If the entry is a regular file */
fileCount++;
else if(entry->d_type == DT_DIR && strcmp(entry->d_name,".")!=0 && strcmp(entry->d_name,"..")!=0)
   countFiles(entry->d_name);
}
// close the dirp
   closedir(dirp);
  
   return fileCount;
}
// main function
int main(int argc,char *argv[]){
// scond argument is fileName
pathP = argv[1];
char *path = argv[1];
// calling function to count files
fileCount = countFiles(argv[1]);
printf("\nTotal Number of Files = %d\n",fileCount);
return 0;
}


screenshot:

This code is compiled and executed in visual studio code ide in ubuntu under LINUX.

EXPLORER C countFiles.cx int fileCount,count; 8 char *pathP; s (ch OPEN EDITORS jay@jay: -/Desktop/c++ 9//function to count t
since step by step with full comments, the code is provided for your full understanding.

However further if any difficulty in understanding the code and approach

then feel free to ask in the comments section.

I will definitely help you.

Add a comment
Know the answer?
Add Answer to:
Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1.Write a bash script A5p1.sh to output the number of executable and non-executable files and subdirectories...

    1.Write a bash script A5p1.sh to output the number of executable and non-executable files and subdirectories separately in the directory that is specified as the first command line argument to this script. Do not count recursively in subdirectories. Do not call any external Linux utilities such as “ls”. 2.Write a bash script A5p1.sh to output the number of executable and non-executable files and subdirectories separately in the directory that is specified as the first command line argument to this script....

  • Write a Java program that lists all the files in a directory and their subdirectories, that...

    Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...

  • write the bash script Write a script compress_large_files.sh. This script accepts one or more command line...

    write the bash script Write a script compress_large_files.sh. This script accepts one or more command line arguments. The first argument has to be an integer; let’s call it size. If this is the only command line argument, compress_large_files.sh inspects all files in the current working directory and compresses every file of size at least size. If there is more than one command line argument, all arguments except the first one must be valid directories. In this case, compress_large_files.sh inspects the...

  • write a bash script that will take one or more directory names as arguments and count...

    write a bash script that will take one or more directory names as arguments and count the number of .txt files under each one (that is the number of .txt files in each subdirectory, subdirectories and so on).

  • Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many...

    Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many files or directories recursively. beside the file counts, also report how many files for each types (directory or regular files). It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide -h option to print out how to use your countf.sh program % ./countf.sh % ./countf.sh file1...

  • Linux commands questions help 1.Find all files under the current directory which ends in '.dat' (just...

    Linux commands questions help 1.Find all files under the current directory which ends in '.dat' (just the .dat not the single quotes as well) and use -exec to create a listing of each file's inode number. 2. Find all files under the /tmp directory whose size is greater than 65 kilobytes and use -exec with an appropriate command to delete these files. 3. Search files in ~ and all subdirectories -- that contain the word 'hawk' (case sensitive) at the...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Select your answer from here. absolute pathname to the file called xyz changes the directory to...

    Select your answer from here. absolute pathname to the file called xyz changes the directory to the parent of the current directory. lists current directory files including the invisible files sends xyz file to the line printer deletes the directory called xyz displays the content of the file called xyz displays the current directory pathname cancels the printing job on the 1p 1 printer confirms the deletion of the xyz file before deleting it lists the current directory in long...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • A filename con comprise multiple embedded dots (e.g., a.b.c.d.e). True False A device file is not...

    A filename con comprise multiple embedded dots (e.g., a.b.c.d.e). True False A device file is not really a stream of characters and doesn't contain anything at all. True False Relative pathnames begin with the root directory. True False Running the ls -a command uniquely identifies directories and binary executables. True False What is the result of running mkdir -p share/man/cat1 from the command line? a. It creates the directory share. b. It creates the directory share/man. c. It creates the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT