0

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)
        //}
    }
}
Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
Bugra Sezer
  • 247
  • 1
  • 5
  • 16
  • To start with you should learn the [`scanf`](http://en.cppreference.com/w/c/io/fscanf) function, as it expects a `char*` (a pointer to a `char`) as argument for the format `"%s"`, and you only give it a single `char` (not a pointer). – Some programmer dude Jan 11 '14 at 20:17
  • oke oke I put & you are write just a small mistake.The rest ? – Bugra Sezer Jan 11 '14 at 20:18
  • You can read directory listing using opendir() function, then You don't have to call "ls" (starting new process is expensive). See http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in-a-directory-using-c-or-c – Roman Hocke Jan 11 '14 at 20:18

1 Answers1

2

Look up opendir etc to read the contents of a directory - http://linux.die.net/man/3/opendir

Look up fopen read read the contents of a file - http://linux.die.net/man/3/fopen

Ed Heal
  • 59,252
  • 17
  • 87
  • 127