Solved

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

Posted on 2004-10-18
16
274 Views
Last Modified: 2010-04-15
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
Comment
Question by:Knightley
16 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12344537
find givenDirectory -name "*.h" -exec someAction {} \;

0
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12344847
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12344910
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
 
LVL 1

Author Comment

by:Knightley
ID: 12345189
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
 
LVL 1

Author Comment

by:Knightley
ID: 12345194
ahh, maybe in should say "write some code" instead of "perform some actions".
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12345203
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
 
LVL 1

Author Comment

by:Knightley
ID: 12345450
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12345509
>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Knightley
ID: 12345547
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
 
LVL 8

Accepted Solution

by:
ssnkumar earned 100 total points
ID: 12345561
>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
 
LVL 1

Author Comment

by:Knightley
ID: 12345581
no, if i have other questions i will open a new thread.

thanx, ssnkumar
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12345594
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
 
LVL 1

Author Comment

by:Knightley
ID: 12345853
i do have to defne DIR *dp;
do i?

e.g.

DIR *dp="directory_of_my_choice"; ???
0
 
LVL 1

Author Comment

by:Knightley
ID: 12345881
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12346019
> 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
 
LVL 1

Author Comment

by:Knightley
ID: 12346176
ok, all clear, thanx again.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now