I'm relearning (OK, "learning" would be a better term since I never really understood it the first time) piping. My end goal is to execute some shell commands like "ls", get the results into another variable, and do some stuff with it. I imagine I'll end up using commands like "fork" "dup2", and "execvp". But right now I'm at the "Hello World" stage. I have this program and (surprise surprise) it doesn't work. I get a seg fault. I didn't expect it to work really, because, if for no other reason, I imagine I have to actually open the "input" and "output" FILE* variables with fopen or whatever, but since they aren't actually files, I'm not sure how.
Can anyone give me a nudge in the right direction? I'm not even sure if this approach makes any sense at all. Here's the code. It seg faults on line 33. Thanks.
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> int main () { FILE* output; FILE* input; int result; int bufferSize; int filedesc[2]; char a[100]; char b[100]; char c[100]; result = pipe (filedesc); if (result) { printf ("Problem. Exiting program.\n"); exit (1); } input = (FILE*) filedesc[0]; output = (FILE*) filedesc[1]; strcpy (a, "Hello World"); bufferSize = strlen (a) + 1; fwrite (a, sizeof (char), bufferSize, output); fread (b, sizeof (char), bufferSize, input); printf ("%s", b); // should display "Hello World" to stdout return 0; }