[C] execvp 사용법 (인자를 args로 전달하기)
2023. 4. 3. 00:33아래와 같이 args를 선언하고 해당 args를 전달하면 된다.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
char* args[] = { "trial", "2", "1", NULL };
if(execvp("./trial", args) == -1) {
printf("\nfailed connection\n");
return 1;
}
return 0;
}
To execute another program which includes a parameter in C, you need to use the execvp function. The execvp function replaces the current process image with a new process image specified by the path of the executable file. The execvp function takes two arguments: the path of the executable file and an array of pointers to null-terminated strings that represent the arguments passed to the program.
First, you need to compile the program that you want to execute using the gcc command. Once the program is compiled, you can pass the path of the executable file as the first argument to the execvp function. The arguments passed to the program should be specified in an array of pointers to null-terminated strings. The last element of the array must be NULL. The first element of the array should be the name of the program itself.
Here is an example of how to use execvp to run a C program from another C program, assuming the program to be executed is called trial.c and takes two arguments:
'C > Code snippet' 카테고리의 다른 글
[C][소켓] 소켓 통신 예제 (Socket) (0) | 2024.09.04 |
---|---|
[C] strlen의 인자로 char* 자료형을 넣었을 때 결과 확인 코드 (0) | 2023.01.27 |
[C] 예제코드: 파일읽기, 삭제 (0) | 2023.01.15 |
[C] 문자열 파싱 (strtok없이), strcpy, strdup, strlen (0) | 2022.07.10 |
[C] exec - execl 예제, c언어 내부에서 ls 같은 시스템 명령어 실행하기 (0) | 2022.06.03 |