As a school project I need to write a C program which opens a folder (folder name taken as parameter) and then writes the contents of the everyfile inside it.Actually doing the job of cat.
But I absolutely dont know how to write it I have knowlage about file opening and systemcalls so I thought using them.
When executing ls command inside exec we used ("bin\ls",ls,NULL) so can I use open or cd command inside execlp to open a folder ?
After opening I need to open every file inside it and print its contents.Probably it will be done with while loop and fopen but how can I write the while loop to check when it came to the end of the folder ? Also even I can manage to write it how the hell I am going to open a file which is name is not written inside the program ?
If systemcalls and fopen is the wrong way then what should I do ?
main(void)
{
FILE * fp;
char dicname[10];
scanf("%s",dicname[0]);
pid_t pid;
pid = fork();
if(pid<0)
{
fprintf(stderr,"Fork error");
exit(-1);
}
else if (pid ==0)
{
//execlp("cd","cd",dicname); or maybe //execlp("open","open",dicname);
//while(end of folder) //maybe 1<argv
//{
//fopen(files,r)
//}
}
}