代碼編譯但不運行,並在第7行和第10行給出錯誤。我無法更正錯誤,請幫助我。 以下是在外殼的錯誤序列:分叉代碼編譯但在執行過程中出現錯誤
./mylab3.c: line 7: char: command not found
./mylab3.c: line 10: syntax error near unexpected token `('
./mylab3.c: line 10: `int main()
代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
char *env_init[]= { "USER=unknown", "PATH=/tmp",NULL};
int main()
{
pid_t pid;
int flag, status;
// Create child process #1
pid = fork();
if (pid < 0)
{
perror("fork error");
}
else if (pid == 0)
{
// Child process – replace with different program
flag = execle("/bin/Is", "Is", "-IF",NULL,env_init);
if (flag < 0)
{
perror("execle error");
}
}
else
{
if (wait(&status) != pid)
{
perror("wait error");
}
}
//Create child process #2
pid = fork();
if (pid < 0)
{
perror ("fork error");
}
else if (pid == 0)
{
flag = execlp("./lab03script","lab03.script","file1", "file2", NULL);
if (flag < 0)
{
perror("execlp error");
}
}
else
{
if (wait(&status) != pid)
{
perror("wait error") ;
}
}
exit (0);
}
'。/ mylab3.c' ??你是否試圖直接運行C文件?請準確顯示您編譯和運行程序的方式。 – kaylum
請正確格式化您的代碼。 –
您需要將源代碼編譯爲可執行文件並運行可執行文件,而不是源代碼 – qrdl