0

Hello ever one I want to ask a question about file opening.I want to write a program in which I will give a path to my main folder.main folder can contain files and and other folders which in again contain other folders and files.I want to open all the file in the loop and do some manipulation on them also I want to open only specific file for example .c extended file.Is there a built in program or function that do that? or atleast is there a way through which I can check which files are there in the folder so that I can repeatedly open them

I am using C programming linux Thanks

5
  • 1
    GNU Simple Directory Lister: gnu.org/software/libc/manual/html_node/… Commented Feb 2, 2012 at 7:51
  • How about the command find? Commented Feb 2, 2012 at 7:54
  • @AlexReynolds thanks so much but is there a way to list all the files i.e. files inside the directories and so on.thanks Commented Feb 2, 2012 at 8:04
  • @JoachimPileborg thanks but in'st find is scripting command ?? correct me if I am wrong I want C program Commented Feb 2, 2012 at 8:12
  • find can find all files of certain criteria (like all files ending with ".c") and then call a program with that file as an argument: find /my/top/path -name '*.c' -exec /my/special/program '{}' \;. It can of course do more advanced filtering. Commented Feb 2, 2012 at 8:15

3 Answers 3

2

You can save yourself a lot of work and look at ftw() and nftw(). They will walk through directories and their entries , starting with the path you provide, and a call a callback function that you provide. In your callback you can check if the file is relevant for purpose and operate on it if it is.

Also glob() will save you some effort if you are going to be doing a lot of filename matching.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not aware of any built-in functions for what you want to do here. But you could use dirent.h.

int main(){
  DIR *dir;
  struct dirent *ent;
  dir = opendir ("c:\\folder\\");
  if (dir != NULL) {

    /* print all the files and directories within directory */
    while ((ent = readdir (dir)) != NULL) {
      printf ("%s\n", ent->d_name);
    } 
    closedir (dir);
  } else {
    /* could not open directory */
    perror ("");
    return EXIT_FAILURE;
  }
}

Here you can find even more examples.

Comments

0

opendir, readdir, closedir.
if dirent->d_type==DT_DIR, descend into it (recursion would help).
Take a look at the file name, figure out if you're interested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.