Thread: Trouble with recursion...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    22

    Exclamation Trouble with recursion...

    I am trying to implement the unix command in my program "ls -R" without the system call.

    Right now, what my recursion code does is like, go to the folder (either ./ or a specified one) and look at the files. If it's a folder, print the name of the folder and call the function again into that folder. Then it does the same thing for that folder.

    The problem is that the output is messed up. When it gets into a folder, it doesn't go back to the like above and prints the remaining files on the last folder. It just prints them after it goes into a folder like this:

    Code:
    : sls -R
    sls.c  backup2sls.c  forfun:
    ffscandir.c  ffscandir.c~  newf:
    blankfold:
     
    soetxt.txt  
    ffa.out  
    backupsls.c  sls.man  a.out  212public  sfouchelink  212shared  sls  makefile  fouche
    The current folder has forfun/, 212public/ and 212shared as symb links, and forfun/newf/ has that soetxt.txt file, and blankfold/ has nothing in it

    This is the code so far:

    Code:
    void rcommand(char *dirPath) {
            ...
            ...
    
    	while((d = readdir(dp)) != NULL)
    	{
    		
    		if(!strcmp(d->d_name,".") || !strcmp(d->d_name,"..")) {
    			continue;
    		}
    
    		//Hiden files/folders
    		if (!acomm){
    			if (*(d->d_name) == '.')
    				continue;
    		}
    
    		if (d->d_type & DT_DIR) {
    			printf("%s:\n", d->d_name);
    			strcpy(temp, dirPath);
    			strcat(temp, "/");
    			strcat(temp, d->d_name);
    			rcommand(temp);
    		} else {
    			printf("%s  ", d->d_name);
    		}
    	}
    	
    	printf("\n");
    	closedir(dp);
    	return;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is no such thing as "going back to the line above" -- you have to make sure you print all the "real" files first, before going into any subdirectories.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    I'm pretty sure you're not going to be able to replace the system call, so that may not be what you mean by "system call". If you're looking to print characters to positions on the screen you can do this in a linux environment from the example below. It sounds like you could just use newlines for your output...?

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    void print_pos(int x, int y, char * msg, ...)
    {
        /* sets the cursor */
        fprintf(stdout, "%c[%d;%df", 0x1B, y, x);
        va_list arg;
        va_start(arg, msg);
        vfprintf(stdout, msg, arg);
        va_end(arg);
    /* You should probably call flush outside of the function */
        /* fflush(stdout); */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion program trouble... ?
    By Beachblue in forum C Programming
    Replies: 7
    Last Post: 06-19-2008, 01:43 PM
  3. trouble with recursion
    By Chaplin27 in forum C++ Programming
    Replies: 17
    Last Post: 10-02-2004, 09:49 PM
  4. #include recursion trouble
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 07-01-2003, 05:15 PM
  5. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM