Transform cat file.txt | wc -l program code c
<p>I'm very newbie about pipes and I want develop a little program for understand and learn about this. My idea consist to communicate command shell cat to wc using c. I was doing a very simple program that use an exiting file (test.txt for example) but for the moment I can only display the content. I only want count the number of lines about 1 specific file.</p>

<p>Is this possible to implement? Or maybe I must do another option? Here my basic code: </p>

<pre><code>int main(int argc, char *argv[]) {
    pid_t pid;
    int fd[2];

    pipe(fd);
    pid = fork();

    if (pid == -1) {
        perror("fork");
        exit(1);    
    }

    if (pid == 0) {
        /* Child process closes up input side of pipe */
        close(fd[0]);
        execlp("cat", "cat", "test.txt", NULL);
        //I don't know how communicate this process with the other process
    } else {
        /* Parent process closes up output side of pipe */
        close(fd[1]);
        execlp("wc", "wc", "-l", NULL);
    }
}
</code></pre>