使用系统ECHO命令以C语言打印2D数组[重复]

时间:2023-01-31 21:29:47

This question already has an answer here:

这个问题在这里已有答案:

How do I execute the system command “echo” in a C program to print out all the elements in a 2D array? I tried it, but it didn't work.

如何在C程序中执行系统命令“echo”以打印出2D数组中的所有元素?我尝试过,但它没有用。

Input:

输入:

char myarray[1][10] = {"Hello", "World"};
system("echo $myarray");

Expected Output:

预期产出:

Hello World

Currently I do not get any output whatsoever.

目前我没有得到任何输出。

Update 1 (constraints): I cannot allocate more memory – I can only use the myarray variable. I may use any other scalar or pointer variables.

更新1(约束):我无法分配更多内存 - 我只能使用myarray变量。我可以使用任何其他标量或指针变量。

3 个解决方案

#1


1  

You need to build the whole command in a single string before calling system. Something like:

在调用system之前,需要在单个字符串中构建整个命令。就像是:

char myarray[2][10] = {"Hello", "World"};
//           ^ notice

char mycommand[42];
strcpy(mycommand, "echo \"");
strcat(mycommand, myarray[0]);
strcat(mycommand, " ");
strcat(mycommand, myarray[1]);
strcat(mycommand, "\"");

system(mycommand);

#2


1  

I'm assuming this is on a Linux machine, not Windows!

我假设这是在Linux机器上,而不是Windows!

The system command echo takes as a parameter what you want it to print out. So you'd think that

系统命令echo将您要打印的参数作为参数。所以你会这么想的

echo $myArray

would print out

会打印出来的

$myArray

but it doesn't. The $ means "Look up the following string in the list of environment variables and print that". And myArray isn't defined in your list of environment variables, so it prints out nothing. It definitely won't print out a variable inside your computer program!

但事实并非如此。 $表示“在环境变量列表中查找以下字符串并打印”。 myArray未在您的环境变量列表中定义,因此它不打印任何内容。绝对不会在您的计算机程序中打印出变量!

If you changed the call to

如果您将通话更改为

system("echo $path");

you would get a long string printed out, listing a number of directories. That's your current path.

你会得到一个长字符串打印出来,列出了许多目录。那是你现在的路。

But none of that answers your problem. What you want to do is print out Hello World using the echo command. That means that you need to (effectively) call the system() function with the following argument:

但这些都不能解决你的问题。您要做的是使用echo命令打印Hello World。这意味着您需要(有效地)使用以下参数调用system()函数:

system("echo Hello world");

The only way you can do that is to "construct" the string that you want it to print out, and then tell system() the complete string.

你能做到的唯一方法就是“构造”你希望它打印出来的字符串,然后告诉system()完整的字符串。

So you need something like the following:

所以你需要以下内容:

#define NUMSTRINGS 2  // Note that there are 2 strings!

char myArray[NUMSTRINGS][10] = {"Hello", "world"};
char command[256] = "echo"; // Start command

int i;
for (i=0; i<NUMSTRINGS; ++i) {
    strcat(command, " ");        // Space separator
    strcat(command, myArray[i]); // Next string
} // for
system(command);

#3


0  

This will not work, because myarray is a variable in C, and $myarray is an environment variable in the shell. They have no relation to one another--just as "Reading, England" and "Reading the news" have no relation.

这不起作用,因为myarray是C中的变量,$ myarray是shell中的环境变量。他们彼此没有关系 - 正如“阅读,英格兰”和“阅读新闻”没有任何关系。

If you want to pass variables to the shell, you can try setenv().

如果要将变量传递给shell,可以尝试使用setenv()。

#1


1  

You need to build the whole command in a single string before calling system. Something like:

在调用system之前,需要在单个字符串中构建整个命令。就像是:

char myarray[2][10] = {"Hello", "World"};
//           ^ notice

char mycommand[42];
strcpy(mycommand, "echo \"");
strcat(mycommand, myarray[0]);
strcat(mycommand, " ");
strcat(mycommand, myarray[1]);
strcat(mycommand, "\"");

system(mycommand);

#2


1  

I'm assuming this is on a Linux machine, not Windows!

我假设这是在Linux机器上,而不是Windows!

The system command echo takes as a parameter what you want it to print out. So you'd think that

系统命令echo将您要打印的参数作为参数。所以你会这么想的

echo $myArray

would print out

会打印出来的

$myArray

but it doesn't. The $ means "Look up the following string in the list of environment variables and print that". And myArray isn't defined in your list of environment variables, so it prints out nothing. It definitely won't print out a variable inside your computer program!

但事实并非如此。 $表示“在环境变量列表中查找以下字符串并打印”。 myArray未在您的环境变量列表中定义,因此它不打印任何内容。绝对不会在您的计算机程序中打印出变量!

If you changed the call to

如果您将通话更改为

system("echo $path");

you would get a long string printed out, listing a number of directories. That's your current path.

你会得到一个长字符串打印出来,列出了许多目录。那是你现在的路。

But none of that answers your problem. What you want to do is print out Hello World using the echo command. That means that you need to (effectively) call the system() function with the following argument:

但这些都不能解决你的问题。您要做的是使用echo命令打印Hello World。这意味着您需要(有效地)使用以下参数调用system()函数:

system("echo Hello world");

The only way you can do that is to "construct" the string that you want it to print out, and then tell system() the complete string.

你能做到的唯一方法就是“构造”你希望它打印出来的字符串,然后告诉system()完整的字符串。

So you need something like the following:

所以你需要以下内容:

#define NUMSTRINGS 2  // Note that there are 2 strings!

char myArray[NUMSTRINGS][10] = {"Hello", "world"};
char command[256] = "echo"; // Start command

int i;
for (i=0; i<NUMSTRINGS; ++i) {
    strcat(command, " ");        // Space separator
    strcat(command, myArray[i]); // Next string
} // for
system(command);

#3


0  

This will not work, because myarray is a variable in C, and $myarray is an environment variable in the shell. They have no relation to one another--just as "Reading, England" and "Reading the news" have no relation.

这不起作用,因为myarray是C中的变量,$ myarray是shell中的环境变量。他们彼此没有关系 - 正如“阅读,英格兰”和“阅读新闻”没有任何关系。

If you want to pass variables to the shell, you can try setenv().

如果要将变量传递给shell,可以尝试使用setenv()。