如何从 c 中获取参数并将其用作函数
How to take argument from c and use it as a function
我想提出一个论点,它是通过终端给我的,并用它来压缩文件。例如,我有 main.c 看起来像这样的文件。
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char **argv)
{
if(argc > 2){
printf("Enough arguments\n");
// tisk argumentu
printf("%s\n\n", argv[1]);
// tisk argumentů
for (int i; i < argc; i++){
printf("%s\n", argv[i]);
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}
然后我通过终端给它这些参数(gzip text1.txt text2.txt),就像这样。
$ gcc main.c -o main
$ ./main gzip text1.txt text2.txt
我的问题是如何获取 argv[1] 中的参数 gzip 并将此函数应用于 text1.txt 和 text2.txt。
这就是我现在拥有的。问题是,execlp 只发生一次,我想使所有参数成为可能(text1.txt,text2.txt --> 稍后超过两个)现在我只能使它成为可能一个 bcs 我在那里硬编码 (argv[2]) --> 那是 text1.txt 并且我需要使 n 个文件成为可能。是否有人能够回答“我应该将 argv[2] 替换为 gzip 所有文件而不是一个文件的原因?”
#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char* argv[])
{
if(argc > 2){
printf("Enough arguments\n");
// printing second argument
printf("Program we use to compress: %s\n\n", argv[1]);
// printing all arguments
for (int i = 0; i < argc; i++){
printf("%d.argument: %s\n", i, argv[i]);
}
int tmp;
for (int i = 0; i < argc + 1; i++){
if (i < 2){
tmp ++;
}
else{
fork();
// execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
execlp(
argv[1],
argv[1],
argv[2],
NULL
);
// if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
int err = errno;
if (err == 2){
printf("File with that name not found or path to that file was wrong!\n");
break;
}
}
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}
pu pravě
#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char* argv[])
{
if(argc > 2){
printf("Enough arguments\n");
// printing second argument
printf("Program we use to compress: %s\n\n", argv[1]);
// printing all arguments
for (int i = 0; i < argc; i++){
printf("%d.argument: %s\n", i, argv[i]);
}
for (int i = 2; i < argc + 1; i++){
// execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
execlp(
argv[1],
argv[1],
argv[i],
NULL
);
// if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
int err = errno;
if (err == 2){
printf("File with that name not found or path to that file was wrong!\n");
break;
}
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}
execv
函数采用 char *
的数组,其中最后一个必须是 NULL
。 argv
数组符合此定义,因此您可以传入 argv[1]
的地址,它为您提供指定的命令及其参数。
此外,您应该检查 fork
的 return 值,并且只在子进程中调用 execv
。
if (fork() == 0) {
// child
execv(argv[1], &argv[1]);
perror("exec failed");
_exit(1);
} else {
// parent
wait(NULL);
}
就是它了!最终工作解决方案
#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char* argv[])
{
if (argc > 2){
printf("Enough arguments\n");
// printing second argument
printf("Program we use to compress: %s\n\n", argv[1]);
// printing all arguments
printf("Arguments we use:\n");
for (int i = 0; i < argc; i++){
printf("%d.argument: %s\n", i, argv[i]);
}
// we start our loop from third argument
for (int i = 2; i < argc + 1; i++){
// splitting our program to n processes
int id = fork();
if (id == 0){
// execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
execlp(
argv[1], // program we use
argv[1], // program we use
argv[i], // the document to which the program is applied
NULL
);
// if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
int err = errno;
if (err == 2){
printf("File with that name not found or path to that file was wrong!\n");
break;
}
}
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}
我想提出一个论点,它是通过终端给我的,并用它来压缩文件。例如,我有 main.c 看起来像这样的文件。
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char **argv)
{
if(argc > 2){
printf("Enough arguments\n");
// tisk argumentu
printf("%s\n\n", argv[1]);
// tisk argumentů
for (int i; i < argc; i++){
printf("%s\n", argv[i]);
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}
然后我通过终端给它这些参数(gzip text1.txt text2.txt),就像这样。
$ gcc main.c -o main
$ ./main gzip text1.txt text2.txt
我的问题是如何获取 argv[1] 中的参数 gzip 并将此函数应用于 text1.txt 和 text2.txt。
这就是我现在拥有的。问题是,execlp 只发生一次,我想使所有参数成为可能(text1.txt,text2.txt --> 稍后超过两个)现在我只能使它成为可能一个 bcs 我在那里硬编码 (argv[2]) --> 那是 text1.txt 并且我需要使 n 个文件成为可能。是否有人能够回答“我应该将 argv[2] 替换为 gzip 所有文件而不是一个文件的原因?”
#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char* argv[])
{
if(argc > 2){
printf("Enough arguments\n");
// printing second argument
printf("Program we use to compress: %s\n\n", argv[1]);
// printing all arguments
for (int i = 0; i < argc; i++){
printf("%d.argument: %s\n", i, argv[i]);
}
int tmp;
for (int i = 0; i < argc + 1; i++){
if (i < 2){
tmp ++;
}
else{
fork();
// execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
execlp(
argv[1],
argv[1],
argv[2],
NULL
);
// if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
int err = errno;
if (err == 2){
printf("File with that name not found or path to that file was wrong!\n");
break;
}
}
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}
pu pravě
#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char* argv[])
{
if(argc > 2){
printf("Enough arguments\n");
// printing second argument
printf("Program we use to compress: %s\n\n", argv[1]);
// printing all arguments
for (int i = 0; i < argc; i++){
printf("%d.argument: %s\n", i, argv[i]);
}
for (int i = 2; i < argc + 1; i++){
// execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
execlp(
argv[1],
argv[1],
argv[i],
NULL
);
// if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
int err = errno;
if (err == 2){
printf("File with that name not found or path to that file was wrong!\n");
break;
}
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}
execv
函数采用 char *
的数组,其中最后一个必须是 NULL
。 argv
数组符合此定义,因此您可以传入 argv[1]
的地址,它为您提供指定的命令及其参数。
此外,您应该检查 fork
的 return 值,并且只在子进程中调用 execv
。
if (fork() == 0) {
// child
execv(argv[1], &argv[1]);
perror("exec failed");
_exit(1);
} else {
// parent
wait(NULL);
}
就是它了!最终工作解决方案
#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...
int main(int argc, char* argv[])
{
if (argc > 2){
printf("Enough arguments\n");
// printing second argument
printf("Program we use to compress: %s\n\n", argv[1]);
// printing all arguments
printf("Arguments we use:\n");
for (int i = 0; i < argc; i++){
printf("%d.argument: %s\n", i, argv[i]);
}
// we start our loop from third argument
for (int i = 2; i < argc + 1; i++){
// splitting our program to n processes
int id = fork();
if (id == 0){
// execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
execlp(
argv[1], // program we use
argv[1], // program we use
argv[i], // the document to which the program is applied
NULL
);
// if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
int err = errno;
if (err == 2){
printf("File with that name not found or path to that file was wrong!\n");
break;
}
}
}
}
else{
printf("Insufficient number of arguments\n");
}
return 0;
}