• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

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,
0
Knightley
Asked:
Knightley
1 Solution
 
brettmjohnsonCommented:
find givenDirectory -name "*.h" -exec someAction {} \;

0
 
pankajtiwaryCommented:
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!
0
 
ssnkumarCommented:
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
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
KnightleyAuthor Commented:
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?
        }
0
 
KnightleyAuthor Commented:
ahh, maybe in should say "write some code" instead of "perform some actions".
0
 
ssnkumarCommented:
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
0
 
KnightleyAuthor Commented:
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.
0
 
ssnkumarCommented:
>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
0
 
KnightleyAuthor Commented:
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?
0
 
ssnkumarCommented:
>char filename[32] = {0};
>does it mean the file name length are limited to 32 characters?
Yes. If you want, you can allocate more space.

>temp_line[strlen(temp_line) - 1] = '\0';
>what does this code do?
fgets() returns a pointer to a string. If it encounters a newline character, that also will be part of the string. So, the above code removes the newline character.
But before doing that, you have to check if it contains a newline like this:
    if (temp_line[strlen(temp_line) - 1] == '\n')
    {
        temp_line[strlen(temp_line) - 1] = '\0';
    }

Any other doubts!?

-ssnkumar
0
 
KnightleyAuthor Commented:
no, if i have other questions i will open a new thread.

thanx, ssnkumar
0
 
ssnkumarCommented:
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
0
 
KnightleyAuthor Commented:
i do have to defne DIR *dp;
do i?

e.g.

DIR *dp="directory_of_my_choice"; ???
0
 
KnightleyAuthor Commented:
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)?
0
 
ssnkumarCommented:
> 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
0
 
KnightleyAuthor Commented:
ok, all clear, thanx again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now