Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C系统can';我不运行hello world_C_Shell_Process - Fatal编程技术网

C系统can';我不运行hello world

C系统can';我不运行hello world,c,shell,process,C,Shell,Process,有人能告诉我为什么shell的这个简单的Csystem调用hello world命令不起作用: MWE: #include <stdio.h> #include <stdlib.h> #include <string.h> main( int argc, char *argv[] ) { char *str; str = strdup ( "hello" ); printf ( "echo %s\n", str ); system

有人能告诉我为什么shell的这个简单的C
system
调用
hello world
命令不起作用:

MWE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main( int argc, char *argv[] )
{
   char *str;

   str = strdup ( "hello" );
   printf ( "echo %s\n", str );
   system ( ( "echo %s\n", str ) );

   return 0;
}
#包括
#包括
#包括
main(int argc,char*argv[])
{
char*str;
str=strdup(“你好”);
printf(“回显%s\n”,str);
系统((“回显%s\n”,str));
返回0;
}
输出:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main( int argc, char *argv[] )
{
   char *str;

   str = strdup ( "hello" );
   printf ( "echo %s\n", str );
   system ( ( "echo %s\n", str ) );

   return 0;
}
回声你好

sh:1:喂:找不到

该行:

system ( ( "echo %s\n", str ) );
尝试运行系统命令“hello”,该命令自然无效

这是因为您使用的是,它只接受最右边参数的值。在本例中,最右边的是
str
,它是指向字符串
“hello”
的指针

(调用
系统时忽略左侧参数
“echo%s\n”


假设您打算打电话:

system("echo hello");
您需要执行以下操作:

char *str;
char outstring[100] = {0};

str = strdup ( "hello" );
sprintf (outstring, "echo %s\n", str);
system (outstring);
sprintf
行将构建字符串
“echo hello\n”
,并将其放在
outstring

然后,
system
调用将执行该命令

(请注意,我将
outstring
的大小设置为固定大小100。如果您的
sprintf
应该生成更多的输出,那么这是不安全的,但出于演示目的,这是最简单的操作)

行:

system ( ( "echo %s\n", str ) );
尝试运行系统命令“hello”
,该命令自然无效

这是因为您使用的是,它只接受最右边参数的值。在本例中,最右边的是
str
,它是指向字符串
“hello”
的指针

(调用
系统时忽略左侧参数
“echo%s\n”


假设您打算打电话:

system("echo hello");
您需要执行以下操作:

char *str;
char outstring[100] = {0};

str = strdup ( "hello" );
sprintf (outstring, "echo %s\n", str);
system (outstring);
sprintf
行将构建字符串
“echo hello\n”
,并将其放在
outstring

然后,
system
调用将执行该命令

(请注意,我将
outstring
的大小设置为固定大小100。如果您的
sprintf
应该生成更多的输出,那么这是不安全的,但出于演示目的,这是最简单的操作)

提示:

  • system()
    (或带有
    %
    的字符串文本)不会像
    printf
  • 双括号使逗号成为一个逗号运算符,它产生正确的参数(
    str
    ),而不是两个参数。C不是Python:-)
提示:

  • system()
    (或带有
    %
    的字符串文本)不会像
    printf
  • 双括号使逗号成为一个逗号运算符,它产生正确的参数(
    str
    ),而不是两个参数。C不是Python:-)

这并不是你想象的那样:

   system ( ( "echo %s\n", str ) );
只返回第二个值,
str
,即
“hello”
。因此,您的程序不会尝试运行
echo hello
,而只运行
hello


您需要使用
sprintf
将整个命令写入一个缓冲区,然后执行该缓冲区。

这与您认为的不同:

   system ( ( "echo %s\n", str ) );
只返回第二个值,
str
,即
“hello”
。因此,您的程序不会尝试运行
echo hello
,而只运行
hello


您需要使用
sprintf
将整个命令写入缓冲区,然后执行该命令。

仅供参考我确实尝试过“用谷歌搜索问题”,但是术语
C
System
未找到
sh
没有返回我正在查找的内容仅供参考我尝试过“用谷歌搜索问题”,但是术语
C
系统
未找到
sh
不返回我正在查找的内容是的,很抱歉,
printf
命令只是显示我正在输出
echo hello
,而不是
derp derp
;-)我不明白,那么我如何告诉它执行
hello world
?出于好奇,
={0}
的目的是什么?它将整个数组初始化为零。严格来说,这不是必需的,因为
sprintf
函数也将以0结束字符串。但是安全是一个很好的做法。是的,我很抱歉,
printf
命令只是显示我正在输出
echo hello
,而不是
derp derp
;-)我不明白,那么我如何告诉它执行
hello world
?出于好奇,
={0}
的目的是什么?它将整个数组初始化为零。严格来说,这不是必需的,因为
sprintf
函数也将以0结束字符串。但安全是一种良好的做法。