For my second project in C we have to write a program to take in 2 arguments, one being a directory path and another being an int level_val. If level_val is -1, files in the parent
directory are to be listed, and so on up the directory tree for -2, -3, etc.
For a positive value k, say, all files are to be listed which are in
directories exactly k subdirectory steps below path_to_start.

I dont know how to parse in the command argument level_val as an int, what i was thinking i could do was just if negative then call chdir("..") in a for loop up to negative value of level_val. Not sure what to do with the positive though...

Anyway, my main question is how do i parse in the argument level_val and store as an int to work in the program? here is my code so far, all it does is print directory path supplied, my code is incomplete because things that i was working on are still in it:


Sorry the clutter in the code, just me testing various things, as you can see I am new to C and how things work.

Thanks in advance for any help.

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>

#define MAX_DIR_PATH 2048

int main(int argc, char *argv[])
{
  unsigned long depth;
  int i = 0;
struct dirent* entry;
char cwd[MAX_DIR_PATH+1];
char* directoryPath;
DIR* dir;

 // if ( argc == 3 ) 
   // {
            
            printf("The value of level_val is: %s\n", argv[2]);
            
            /*DIR* dir = opendir(argv[1]);
            if(!dir){
                    perror("opendir");
                    exit(1);
                    }*/
            directoryPath = argv[1];
            
            if (chdir(directoryPath) == -1) 
            {
                     fprintf(stderr, "Cannot change to directory '%s': ", directoryPath);
	                 perror("");
	                 exit(1);
            }

            if (!getcwd(cwd, MAX_DIR_PATH+1)) 
            {
                             perror("getcwd:");
	                         return;
            }
            
            /*if(argv[2] < 0)
            {
                     printf("in first if\n");
                     while(i != argv[2])
                     {
                             printf("in here");
                             printf("In if\n");
                          if(!chdir(".."))
                          {
                                          printf("in second if");
                                          perror("chdir (1)");
                                          exit(1);     
                          }
                                          
                        i--;
                     }
            }*/
         
                   
            dir = opendir(".");
            if (!dir) 
            {
	                  fprintf(stderr, "Cannot read directory '%s': ", cwd);
	                  perror("");
                   	return;
            }

            
            printf("Directory conents:\n");
            while( (entry = readdir(dir)) != NULL)
            {
                   
                   printf("%s\n", entry->d_name);
            }
            
            
            
            printf("new working directory\n");
            printf("Directory conents:\n");
            while( (entry = readdir(dir)) != NULL)
            {
                   printf("%s\n", entry->d_name);
            }
    /*else{
         
         printf("Not enough arguments supplied to run program.");
  
}*/
  
  
  system("PAUSE");	
  return 0;
}

thanks dragon, figured it was something easy like that but for some reason couldnt find online. do you know how to change directories into a subdirectory, I guess for my assignment it would be a folder that contains only one folder and i could check to see if it could be opened

You helped me out on my last C++ thing as well, thanks a lot

Its not necessary to call chdir() in order to complete your assignment.
Example code snippet here. That code snippet recursively parses the entire directory path without using chdir(). All you have to do is stop the recursion when the appropriate path number has been found, then list all the files in that directory.

That code snippet was written for c++, not C, but the concepts are nearly the same. To convert it to C just delete the vector parameter and pass only the string as a character array.

Okay, thanks for the help so far, I know how to change the depth into the int, but all I can do right now is open up the given folder path and print those files, I looked at the code example you gave me for a while and dont quite understand how I would manipulate that to reach my project requirements... Basically, I dont know how to change to a different folder and then print those files in that folder, i was trying that chdir(".."), but that does not work?

thanks a lot, sorry about this but our teacher really hasnt covered any of this for the project so im trying to read online and the book, but not really coming up with a solution, need some guidance.

try finding pdf version of "Beginning Linux Programming by Neil Matthew and Richard Stones" or buy a hard copy. This book covers all your issues well.

You don't have to use chdir() because in the code snippet I posted the function parameter is the full path to the directory. Here's one way to do it (in C, not C++)

Note: I did not compile or test the program below.

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

using namespace std;

int seekdir(char* path, int depth)
{
    static int count = 0;
    DIR* dir;
    count++;
    dir = opendir(path.c_str());
    if(dir == 0)
    {
        cout << "Can not open directory '" << path << "'\n";
        return 1;
    }
    struct dirent *d;
    while( (d = readdir(dir)) != 0)
    {
        if( d->d_type == DT_DIR && strcmp(d->d_name,".") != 0 &&
                strcmp(d->d_name,"..") != 0)
       {
            char* p = malloc(255);
            strcpy(p,path);
            strcat(p,"/");
            strcat(p,d->d_name);
            seekdir(p);	
            free(p);	
        }
        else if(count == depth && d->d_type == DT_REG)
        {
             printf("%s\n",d->d_name);
        }

    }
    closedir(dir);	
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.