I'm having trouble to manipulate directories in C.
- I want to give the name of 2 directories as argument on main
- check if the first directory exists (in the current path)
- open the directory
- call a function (that i created) to create files and do stuff inside the directory
- close the directory and go into the 2nd directory and do the same .
I wrote my code but it still not doing the stuffs inside the directories that i gave on main, instead it looks like i'm always positioned in the current directory, so is the call to open the directory not good???
Here's what I've done :
int main(int argc, char *argv[])
{
int i = 0;
char cwd[1024];
if(argc < 3)
{
printf("Erreur dans les arguments\n");
} else
{
for(i = 1; i < argc; i++)
{
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
printf("an error occured when getting current directory\n");
}
// make a path to the directory
strcat(cwd, "/");
strcat(cwd, argv[i]);
strcat(cwd, "/");
printf("cwd %s\n", cwd);
//check if directory exist and readable
//if((rep = opendir(argv[i])) != NULL) not working also
if((rep = opendir(cwd)) != NULL)
{
getcwd(cwd, sizeof(cwd));
printf("cwd %s\n", cwd);
// do some stuff on the directory
//int result = createFile("file.txt"); // this function works fine but the file is always created in the current directory
}
}
}
}
if anyone could help, it will be appreciated. Thank u.