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
  • 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 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...

  • 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...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • 1) Write a C program that displays all the command line arguments that appear on the...

    1) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 2) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that...

  • C++ Example 1 of command and output Example 3 of command and output You will create...

    C++ Example 1 of command and output Example 3 of command and output You will create a C++ program that can read the arguments, according to the arguments, your program should output some patterns accordingly. The C++ source codes will become the executable to be tested by the TAs. The result should be outputted to the console. The general call to the executable is as follows in Linux: ./main 1 2 Linux Command: ./main 1 2 Linux Command: ./main 5...

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
ADVERTISEMENT