0

So far I have this which I found here on Stack Overflow, but this only displays a file from the path given in the program, not provided by the user.

I've tried to add variables to the printfile function but with no effect.

int findfile_recursive(const char *folder, const char *filename, char *fullpath )
{
    char wildcard[MAX_PATH];
    sprintf(wildcard, "%s\\*", folder);
    WIN32_FIND_DATA fd;
    HANDLE handle = FindFirstFile(wildcard, &fd);
    if(handle == INVALID_HANDLE_VALUE) return 0;
    do
    {
        if(strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
            continue;
        char path[MAX_PATH];
        sprintf(path, "%s\\%s", folder, fd.cFileName);

        if(_stricmp(fd.cFileName, filename) == 0)
            strcpy(fullpath, path);
        else if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            findfile_recursive(path, filename, fullpath);
        if(strlen(fullpath))
            break;
    } while(FindNextFile(handle, &fd));
    FindClose(handle);
    return strlen(fullpath);
}

int printfile(void)
{
    char a,b;
    printf("Path: ");
    gets(a);
    printf("Name: ");
    gets(b);
    char fullpath[MAX_PATH] = { 0 };
    if(findfile_recursive(&a, &b, fullpath)){
        printf("found: %s\n", fullpath);
    }
    else{
        printf("Nothing found");
    }

}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Taco
  • 45
  • 9
  • 3
    First of all, never ***ever*** use `gets`, it's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function that have even been removed from the C specification. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead (but note the differences between `gets` and `fgets` and what they put in the buffer you provide). – Some programmer dude Apr 14 '20 at 09:34
  • 3
    Secondly, both `gets` and `fgets` takes as argument a pointer to the first element of an array of characters. How could you fit a path in a single `char`? Doesn't the compiler warn you about passing wrong arguments to `gets`? And that you need to use the address-of operator when calling `findfile_recursive` should be an indicator that you do something wrong. – Some programmer dude Apr 14 '20 at 09:34
  • No, the compiler did not display any errors. – Taco Apr 14 '20 at 09:37
  • Okay,i've edited the code, now variable can fit a path or filename. – Taco Apr 14 '20 at 09:43
  • 3
    Please don't "fix" the code in the question, especially once you have gotten an answer. By "fixing" the code your question no longer have the problems, or at least not the same problem, so it's now useless for others. – Some programmer dude Apr 14 '20 at 09:45
  • I just figured out a solution, where can I share it for others? – Taco Apr 14 '20 at 09:51
  • You can always post an answer yourself. – Some programmer dude Apr 14 '20 at 09:56

2 Answers2

1
char a,b;
printf("Path: ");
gets(a);
printf("Name: ");
gets(b);

gets returned a pointer to char but you are using a char, also notice that gets is deleted from the standard, you sould replace it with fgets and strip the trailing newline leaved by the function.

char str[1024];                    // Space enough to store a path
printf("Path: ");
if (fgets(str, sizeof str, stdin)) // read stdin
{
    char *ptr = strchr(str, '\n'); // Find the trailing newline
    if (ptr != NULL)
    {
        *ptr = '\0';               // Strip the trailing newline
    }
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

After corrections, this code does its job.

int findfile_recursive(const char *folder, const char *filename, char *fullpath )
    {
        char wildcard[MAX_PATH];
        sprintf(wildcard, "%s\\*", folder);
        WIN32_FIND_DATA fd;
        HANDLE handle = FindFirstFile(wildcard, &fd);
        if(handle == INVALID_HANDLE_VALUE) return 0;
        do
        {
            if(strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
                continue;
            char path[MAX_PATH];
            sprintf(path, "%s\\%s", folder, fd.cFileName);

            if(_stricmp(fd.cFileName, filename) == 0)
                strcpy(fullpath, path);
            else if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                findfile_recursive(path, filename, fullpath);
            if(strlen(fullpath))
                break;
        } while(FindNextFile(handle, &fd));
        FindClose(handle);
        return strlen(fullpath);
    }

    int printfile(void)
    {
        char name[1024], path[1024];
        printf("\n\nFilename: ");
        scanf("%s",name);
        printf("Path: ");
        scanf("%s",path);
        char fullpath[MAX_PATH] = { 0 };
        if(findfile_recursive(&path,&name, fullpath)){
            printf("Found: %s\n", fullpath);
        }
        else{
            printf("Nothing found\n");
        }
    }
Taco
  • 45
  • 9