0

in one of my exercice I have to open a folder thanks to C. I do this with the library of dirent, after that I success to display to the screen the name of every file in the Folder. But now, I want to open every time the current file that have been print to the screen. So I do this with a fopen but every time my fopen returns me NULL what can I do in order to open every time the current file. Thanks you !

Here is my code :

void OpenDirectory(char *Folder, char *Virus){
DIR * dir = NULL;
struct dirent* CurrentFile = NULL;

dir = opendir(Folder);
if (dir == NULL){
    perror("");
}
else
{
    while ((CurrentFile = readdir(dir))!= NULL)
    {
        if (!strcmp(CurrentFile->d_name, ".") || !strcmp(CurrentFile->d_name, ".."))
        {
            // Don't do anything
        }
        else
        {
            printf("Current file is : %s\n", CurrentFile->d_name);
            Myfile(CurrentFile->d_name);
        }
    }
}


}

void Myfile(FILE* File1){
File1 = fopen(File1, "rb");
printf("File is : %s", File1);
if (File1 == NULL)
{
    printf("The file source doesn't exist !\n");
}
else
{
    printf("Welcome");
}
}
5
  • 1
    Show the actual print out, ie. what file specification is printed out by 'printf("File is : %s", File1);'. Obviously, for the fopen() to work, this needs to be a valid name AND PATH. Commented May 21, 2016 at 20:35
  • How can I do because the folder who contains all the file is not in the same directory of my program .c Commented May 21, 2016 at 20:38
  • You must append the filename to the specification of the folder containing it, (strcat). Commented May 21, 2016 at 20:39
  • Also, using the same var name: 'File1', for both the fine specification and the file descriptor token in Myfile() is VERY confusing! Commented May 21, 2016 at 20:41
  • CurrentFile->d_name is an array of char - passing it to a function which expects a FILE * (not to mention passing a FILE * to printf() when it's expecting a char *, and before you've checked it against NULL) is only going to lead to tears. Your compiler should be screaming warnings at you. Commented May 21, 2016 at 23:50

1 Answer 1

1

The directory contains filenames.

So the first filename might be foo, and that's what you will get out of the dirent. So then you try to open it, effectively doing fopen("foo", "r");.

But that means "Open the file named foo in the current working directory". Since the file is in some subdirectory, it won't be found.

One solution is to cwd to the directory each time you start to work on a new directory. That works fine; the challenge is returning to the parent directory when you are done.

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

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.