Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-18
16
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

618 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