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");
}
}
CurrentFile->d_nameis an array ofchar- passing it to a function which expects aFILE *(not to mention passing aFILE *toprintf()when it's expecting achar *, and before you've checked it againstNULL) is only going to lead to tears. Your compiler should be screaming warnings at you.