C Program to read all the files located in the specific directory

Description

Write a C Program should read the contents of files which is loacted in the directory.

Solution

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    DIR *d;
    struct dirent *dir;
    FILE *fptr;
    char ch;
    char content[100000];


    d = opendir("E:/test");
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
        	
	    	fptr = fopen(dir->d_name, "r");
		if (fptr != NULL)
    		{
			printf("%s\n", dir->d_name);
       			ch = fgetc(fptr);
    			while (ch != EOF)
    			{
        			printf ("%c", ch);
        			ch = fgetc(fptr);
    			}
			printf("\n\n");
    			fclose(fptr);
        	}
		
        }
        closedir(d);
    }
    return(0);
}

We are Teaching "Web Development using Python" through Online.

View Tutor Profile and Course Syllabus

Explanation

 is the header file has functions to process the directory.
Opendir() function opens the directory
dit->d_name gives the entries of the directory.
If the entry is a file then it is processed using file operations.
Ramesh P Natarajan

Author - Ramesh P Natarajan

Software Developer and Technology Trainer, very much passionate in the field of Software Application Development, Machine Learning and Search Engine Optimization.

Write to: [email protected]





Interesting Pattern Programs: for more Patterns

Input:
5
Output:
*
**
***
****
*****
****
***
**
*Solution
Input:
5
Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *Solution
Input:
5
Output:
*        *
**      **
***    ***
****  ****
**********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *Solution
Input:
5
Output:
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********Solution