I'm just starting to learn C programming and I have some uncertainty about fork(), exec(), pipe(), etc.
I've developed this code, but when I execute it, the variable c remains empty, so I don't know if the child isn't writing to the pipe, or the parent isn't reading from it.
Could you help me please? This is the code:
int main() { int pid=0; int pipefd[2]; char* c=(char *)malloc(sizeof(char)); FILE *fp; pipe(pipefd); pid=fork(); if (pid==0){ close(pipefd[0]); dup2(pipefd[1],1); close(pipefd[1]); execl("ls -l | cut -c28","ls -l | cut -c28", (char *) 0); } else{ close(pipefd[1]); read(pipefd[0], c, 1); char* path="/home/random"; char* txt=".txt"; char* root=malloc(strlen(path) + strlen(txt) + sizeof(char)); strcpy(root,path); strcat(root,c); strcat(root,txt); close(pipefd[0]); fp=fopen(root,"w+"); (...) } The problem is that the final root string its only "/home/random.txt" because there is nothing in the char c, and what I want is to open the file "/home/random(number stored in char c).txt".
execlcall isn't returning - I don't think you can callexecllike that.