나는 exec() 함수를 호출하고 그것의 출력을 tmp 파일에 저장하는 cat 명령을 구현하려고한다. 내 문제는 exec()를 호출 한 후에 이후에 아무 것도 무시된다는 것을 알기 때문에 exec()를 루핑 할 필요가 없다는 것입니다.for 루프에서 exec()하는 방법은 무엇입니까? C에서
주 프로그램에 전달할 N 개의 인수가있는 경우 모든 인수를 읽으려면 exec()를 반복 할 수 있습니까?
참고 : system()을 사용하는 것은 나를위한 선택이 아니므로 할당 방법이 다릅니다.
는 지금 내가하지 매우 우아한 방법으로 다음과 같은 코드가 있습니다 :
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/wait.h>
int main(int argc,char *argv[])
{
int fd;
char filename[] = "tmp.txt";
fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
dup2(fd, 1); // make stdout go to file
dup2(fd, 2); // make stderr go to file
close(fd);
execl("/bin/cat", argv[0], argv[1], argv[2], argv[3], NULL);
return(0);
}
당신이 바로, 구문 분석, 통과하지 의미? –
그리고'execv()'는 당신이 원하는대로 행동해야합니다. https://linux.die.net/man/3/execv –
@MarkYisri, 맞습니다, 제 철자. – krm