How to write a C Program to Recursive function that searches for a given file in a given folder in C Programming Language ?
Input:
- Char pointer to the path folder.
- Char pointer to the target file name.
- Char pointer to the type of file to search for.
Solution:
/* * Recursive function that searches for a given file in a given folder. * input: Char pointer to the path folder. * Char pointer to the target file name. * Char pointer to the type of file to search for. */ void find_file(char* path, char* target, char typeValue){ DIR *dir; struct dirent *entry; if (!(dir = opendir(path))){ perror("Could not open directory."); return; } while ((entry = readdir( dir )) != NULL) { if ( !strcmp( entry->d_name, "." )) continue; if ( !strcmp( entry->d_name, ".." )) continue; struct stat s; char fullpath[strlen(path) + strlen(entry->d_name) + 2]; sprintf(fullpath, "%s/%s", path, entry->d_name); lstat(fullpath, &s); // If entry is a directory - add directory to list if (S_ISDIR(s.st_mode)) { addFolderToList(fullpath); } // Switch over type value, only print matches with correct type if (strcmp(target, entry->d_name) == 0) { switch (typeValue){ case ('a'): printf("%s \n", fullpath); break; case ('d'): if (S_ISDIR(s.st_mode)) printf("%s \n", fullpath); break; case ('l'): if(S_ISLNK(s.st_mode)) printf("%s \n", fullpath); break; case ('f'): if(S_ISREG(s.st_mode)) printf("%s \n", fullpath); break; } } } closedir(dir); }
Learn More :
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program File Input & Output Example
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- C Program To Write Data In A File And Search Data From File
- Make a copy of file given in argv[1] to file given in argv[2]
- C Program to copy a file to another file
- C Function to read instructions from the file and obey them
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- File Number Control C Program Example
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Recursively converts all file-names to lower-case
- C Program to Find the Size of File using File Handling Function
- C Program Read a char & print next char ( file to file )
- C Program to Input Student Details into a File For Two Subjects
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program
Recursive
Function
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program Character toupper() Example
- C Program Function Example
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program To Convert Temperature In Celsius To Fahrenheit, Using Function
- C Program To Find Simple Interest
- C Function to Check Vowel
- Factorial Program In C Using Function
- C Program For Prime Number Using Function
- C Function to xorSwap and addSwap in C Programming
- C Program to concatenate two strings without using string functions
- C Function to read instructions from the file and obey them
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Implements a dictionary's functionality.
- = (int*)malloc(sizeof(int)) ??
- Design a C function that shortens a string to 8 characters
- C Program to Implements a dictionary's functionality
- C Program that prompts the user to input a string, (whitespaces allowed)
- Local sounds functions in C Program
- Function Get the resource that will be gathered from the zone name
- C Program to Find the Size of File using File Handling Function
- Average function in C
given
- C Program To Find Week Day Of A Given Date
- Give me an integer and I will sum it with the previous natural numbers
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Check Given String is Palindrome or Not
- C Program That in a Given Natural Number x Insert Figure c at Position p
- Return the number of tokens given a command
- C Program reads in and spews out the attributes of a given .wav file.
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C program to display the transpose of given 3 X 3 matrix
- C Program to convert given decimal number into binary number
Folder
Search
- Dictionary Word Search C Program
- C Depth First Search
- C Program To Search A Number Inside The Array
- C Program To Write Data In A File And Search Data From File
- C Program to search an element using linear search or binary search (menu driven program)
- C Program Performs a search and replace for a specified target and replacement string
- Pre Order, Post order, In order Implement Binary Tree using linked list
- BST Tree in C
- C Program Search Function For Sequence
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list