据我了解,argv [0]是程序的路径。但是,我们正在做一个赋值,并且我的一个朋友在调用argv [0]时得到了第一个参数的名称。
为什么会发生这种情况,我该如何更改这种行为?
编辑:这是父流程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE 200
int main(int argc, char const *argv[])
{
char fileName[SIZE];
int bytesToRead;
int status;
bytesToRead = read(0 ,fileName, SIZE);
int p[2];
pipe(p);
pid_t pid;
if((pid = fork()) == -1){
perror("error en el fork");
}
else if(pid == 0){
close(p[0]);
dup2(STDOUT_FILENO, p[1]);
execl("./printTest", fileName, NULL);
close(p[1]);
exit(0);
}
else{
}
waitpid(pid, &status, 0);
return 0;
}
接下来是一个子进程:
#include <stdio.h>
#include <unistd.h>
#define SIZE 512
int main(int argc, char const *argv[])
{
//char buffer[SIZE];
//int bytesToRead;
/*
while((bytesToRead = read(0 ,buffer, SIZE)) > 0){
write(1, buffer, bytesToRead);
}*/
printf("%s\n",argv[0]);
printf("Exec executed\n");
return 0;
}
据我们了解,argv [0]应该保留程序的名称,但它会打印第一个参数(无论父进程中stdin输入什么)