Link to home
Start Free TrialLog in
Avatar of Knightley
Knightley

asked on

How to open *.h files in a directory (incl. all sub directories)

Hi,

What i would like to do is search in a given directory incl. all the sub directories
for all *.h files, and performing same actions for each .h file.

something like:
++++++++++++++++++++++++++++++++++++++++++
Do "search for *.h files in directory and sub directories"
      perform some actions
While "there are still files/directories left"
++++++++++++++++++++++++++++++++++++++++++

Could any one give me an example?

any help appreciated,

k,
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

find givenDirectory -name "*.h" -exec someAction {} \;

Avatar of pankajtiwary
pankajtiwary

Hi Knightley,

Try this.

#!/bin/bash

for file in `find /usr/include -name *.h 2>/dev/null`
        do tail $file   # Or whatever you want to do.
done;

Cheers!
Avatar of Narendra Kumar S S
Hi Knightley,

   Do you want to do this with a shell script/command or with a C program?

   If you want to do it with a C program, then do like this:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

main(int argc, char **argv)
{
        DIR *dp;
        struct dirent *myfiles;

        dp = opendir(argv[1]);
        if (dp == NULL)
        {
                printf("Error in opening the directory: %s\n", argv[1]);
                exit(0);
        }

        while ((myfiles = readdir(dp)) != NULL)
        {
                printf("%s\n", myfiles->d_name);
        }
        closedir(dp);
}

Hope this helps.....

-ssnkumar
Avatar of Knightley

ASKER

as a C program/function, i thought this is clear while i am
in the Language C section. :)

        while ((myfiles = readdir(dp)) != NULL)
        {
                printf("%s\n", myfiles->d_name);
                //DO I ADD MY CODES HERE?
        }
ahh, maybe in should say "write some code" instead of "perform some actions".
So, did you try my code!?
Yes, inside the while loop, myfiles->d_name will get you the file names in the given directory.
Each time through the loop it gets a new file.
This goes on till it gets all the files.
Note that, it also gets you the directories "." and ".." and also all the subdirectories.

-ssnkumar
now am in the office, the code i want to add are something like:

FILE *fp;
fp = fopen("version.h", "r");
do
  {
    line = fgets(temp_line, line_L, fp);
    if (line ==search_word){
    break;
    }
  } while (c != NULL);
fclose(fp);

version.h is a example file, and there are other .h files,
so i should modify the code to:

fp = fopen(myfiles->d_name, "r"); ?

Does myfiles->d_name also includes the sub directory name?
or is it only the file name? Or do i need include the subdirectory name at all?

P.S. i have no c compiler on my current OS, so could not verify,
and also feeling quite embarrassed.
>Does myfiles->d_name also includes the sub directory name?
No. It is just the filename.
You will have to concatenate the subdirectory name.

And in your sample code, you have used:
>   if (line ==search_word){
If you are matching two character strings, then use strcmp(), like this:
   if (strcmp(line, search_word) == 0){

Hope that is just a pseudo-code.

So, my modified code would be:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

main(int argc, char **argv)
{
        DIR *dp;
        struct dirent *myfiles;

        dp = opendir(argv[1]);
        if (dp == NULL)
        {
                printf("Error in opening the directory: %s\n", argv[1]);
                exit(0);
        }

        while ((myfiles = readdir(dp)) != NULL)
        {
               FILE *fp;
               char filename[32] = {0};

               printf("%s\n", myfiles->d_name);

               sprintf(filename, "%s//%s\n", argv[1], myfiles->d_name);

               fp = fopen(filename, "r");
               do
               {
                   line = fgets(temp_line, line_L, fp);
                   if (line == NULL) break;

                   temp_line[strlen(temp_line) - 1] = '\0';
                   if (strcmp(temp_line, search_word) == 0)
                   {
                       break;
                   }
               } while (line != NULL);

               fclose(fp);
        }
        closedir(dp);
}

-ssnkumar
not pseudo code unfortunately.

char filename[32] = {0};
does it mean the file name length are limited to 32 characters?

temp_line[strlen(temp_line) - 1] = '\0';
what does this code do?
ASKER CERTIFIED SOLUTION
Avatar of Narendra Kumar S S
Narendra Kumar S S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
no, if i have other questions i will open a new thread.

thanx, ssnkumar
So, you got all your doubts cleared!?
I think it is better for you to try the code and give feedback.

If you are really satisfied, then you can close the thread:-)

-ssnkumar
i do have to defne DIR *dp;
do i?

e.g.

DIR *dp="directory_of_my_choice"; ???
main(int argc, char **argv)

do i have to set the parameters?

if this is a sub function, how should i call it?

say the function is called (i added a 3. parameter),

scan_h_file(int argc, char **argv, int i)

should i initiate it by:

scan_h_file(int argc, char **argv, 4)?
> i do have to defne DIR *dp;
>do i?
YES. You have to.

>DIR *dp="directory_of_my_choice"; ???
That is not correct. Do like this:
DIR *dp = opendir("directory_of_my_choice");

If you want to put it as a function, you can do like this:
scan_h_file(char *directory_name)
{
        DIR *dp;
        struct dirent *myfiles;

        dp = opendir(directory_name);
        if (dp == NULL)
        {
                printf("Error in opening the directory: %s\n", directory_name);
                exit(0);
        }

        while ((myfiles = readdir(dp)) != NULL)
        {
               FILE *fp;
               char filename[32] = {0};

               printf("%s\n", myfiles->d_name);

               sprintf(filename, "%s//%s\n", directory_name, myfiles->d_name);

               fp = fopen(filename, "r");
               do
               {
                   line = fgets(temp_line, line_L, fp);
                   if (line == NULL) break;

                   temp_line[strlen(temp_line) - 1] = '\0';
                   if (strcmp(temp_line, search_word) == 0)
                   {
                       break;
                   }
               } while (line != NULL);

               fclose(fp);
        }
        closedir(dp);
}

To call it from main:

main()
{
     char mydir[128] = {0};

     printf("Enter directory path: ");
     scanf("%s", mydir);
     scan_h_file(mydir);
}

Hope this is what you asked.....

-ssnkumar
ok, all clear, thanx again.