Functions calling themselves? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Functions calling themselves?

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
I am taking a day or two off from my world editor to build a simple (actually, rebuild) temporary file remover, which will delete all the files in "WindowsTEMP" and in any of it's subfolders, as well as the actual subfolder. I have two questions about this though. When FindFirstFile or FindNextFile return, they only return the file or directory name, and I need the full drive and path to delete files and subfolders. Second, can a function call on itself? The way I plan on doing it is this (this is my actual code so far):
[code]
void remove(char place[512])
{
WIN32_FIND_DATA fd;

fn = FindFirstFile(place, &fd);

if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
/*
First get the full path and directory name
Now call remove() with that info
Once it returns, you can delete the empty folder!
*/
}
else
//Just delete the file, if not a dir

//Code in a loop with FindNextFile here!
return;
}

void clean(HWND cwnd)
{
if(strstr(paths[0], "Not used.") == NULL)
remove(paths[0]);
return;
}
[/code]
Can somebody please help me out with this? I appreciate it.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

Comments

  • reunionreunion Posts: 12Member
    : I am taking a day or two off from my world editor to build a simple (actually, rebuild) temporary file remover, which will delete all the files in "WindowsTEMP" and in any of it's subfolders, as well as the actual subfolder. I have two questions about this though. When FindFirstFile or FindNextFile return, they only return the file or directory name, and I need the full drive and path to delete files and subfolders. Second, can a function call on itself? The way I plan on doing it is this (this is my actual code so far):

    Ok. There is nothing wrong with functions calling themselves.

    [code]
    int factorial(int n)
    {
    return ((n <= 0) ? 1 : n * factorial(n - 1));
    };
    [/code]

    Like this one. It is often more effective when functions call themselves then using cycles.
    Though, every calling of a function costs some time, of course. Using cycles is faster.

    Very well, I think with our 1GHz proccessors it doesn't matter at all. ;o)

    [green]Reunion[/green]



  • weiccoweicco Posts: 950Member
    So you're talking about recursive function call. This is very handy in a case like deleting subfolders and contents. Here's my code that finds all .mp3 files and changes their attributes. I think there's some bug in it but you get the picture.

    void searchloop(void) {
    long res;
    struct _finddata_t fileinfo;

    res = _findfirst("*.*", &fileinfo);

    while(1) {
    if((strcmp(fileinfo.name, ".") != 0)&&(strcmp(fileinfo.name, "..") != 0))
    if(_chdir(fileinfo.name) == 0) {
    searchloop();
    } else {
    if(!strcmp(fileinfo.name, ".mp3")) {
    SetFileAttributes(fileinfo.name, FILE_ATTRIBUTE_NORMAL);
    }
    }
    if(_findnext(res, &fileinfo) < 0)
    break;
    }
    _chdir("..");
    }

    : I am taking a day or two off from my world editor to build a simple (actually, rebuild) temporary file remover, which will delete all the files in "WindowsTEMP" and in any of it's subfolders, as well as the actual subfolder. I have two questions about this though. When FindFirstFile or FindNextFile return, they only return the file or directory name, and I need the full drive and path to delete files and subfolders. Second, can a function call on itself? The way I plan on doing it is this (this is my actual code so far):
    : [code]
    : void remove(char place[512])
    : {
    : WIN32_FIND_DATA fd;
    :
    : fn = FindFirstFile(place, &fd);
    :
    : if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    : {
    : /*
    : First get the full path and directory name
    : Now call remove() with that info
    : Once it returns, you can delete the empty folder!
    : */
    : }
    : else
    : //Just delete the file, if not a dir
    :
    : //Code in a loop with FindNextFile here!
    : return;
    : }
    :
    : void clean(HWND cwnd)
    : {
    : if(strstr(paths[0], "Not used.") == NULL)
    : remove(paths[0]);
    : return;
    : }
    : [/code]
    : Can somebody please help me out with this? I appreciate it.
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]
    :


Sign In or Register to comment.