My C code is giving me an error expected identifier or \'(\' on line 10 which is
ID: 3789296 • Letter: M
Question
My C code is giving me an error expected identifier or '(' on line 10 which is an array declaration
Can anyone get this to run
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define RD 0
#define WT 1
int main()
{
int fd1[2]={0,1};
int fd2[2]={0,1};
pipe(fd1);
switch(fork()) {
case -1:
perror("Fork error");
break;
case 0: // child does the ls
close(fileno(stdout));
dup(fd1[WT]);
close(fd1[WT]);
close(fd1[RD]);
execlp("ls", "ls", NULL);
break;
default:
pipe(fd2);
switch(fork()) {
case -1:
perror("Fork error");
break;
case 0:
close(fileno(stdin));
dup(fd1[RD]);
dup(fd2[RD]);
close(fd1[WT]);
close(fd1[RD]);
close(fd2[WT]);
close(fd2[RD]);
execlp("grep", "grep", ".c", NULL);
break;
default:
dup(fd2[RD]);
close(fd2[WT]);
close(fd2[RD]);
execlp("tail", "tail", "-4", NULL);
break;
}
}
return 0;
}
Explanation / Answer
//Following is the correct code for given problem
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define RD 0
#define WT 1
int main()
{
int fd1[2]={0,1};
int fd2[2]={0,1};
pipe(fd1);
switch(fork()) {
case -1:
perror("Fork error");
break;
case 0: // child does the ls
close(fileno(stdout));
dup(fd1[WT]);
close(fd1[WT]);
close(fd1[RD]);
execlp("ls", "ls", NULL);
break;
default:
pipe(fd2);
switch(fork()) {
case -1:
perror("Fork error");
break;
case 0:
close(fileno(stdin));
dup(fd1[RD]);
dup(fd2[RD]);
close(fd1[WT]);
close(fd1[RD]);
close(fd2[WT]);
close(fd2[RD]);
execlp("grep", "grep", ".c", NULL);
break;
default:
dup(fd2[RD]);
close(fd2[WT]);
close(fd2[RD]);
execlp("tail", "tail", "-4", NULL);
break;
}
}
return 0;
}
Output :
Lenovo-G580:~/codes/schegg$ gcc ccode.c
Lenovo-G580:~/codes/schegg$ ./a.out
ccode.c
chessboard.cpp
ChunkList.cpp
counter.c
Driver.cpp
first.c
first.cpp
fork_search.c
InteractiveDrawingApplication.java
Item.class
Main$1.class
MyRect.java
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.