系统命令 [英] system command

查看:59
本文介绍了系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在编写一个使用system()命令的脚本。在

脚本中,system命令用系统调用加密(gpg -f

file.txt)。


当需要脚本对用户通过另一个

函数选择的不同文件执行此b / b $ b操作时,问题就出现了。文件名的名称作为

字符串存储在变量中,我无法将变量替换为ststem

命令。我试图将

引号中的整个命令连接成一个字符串变量并将其输入系统命令

但是这会出现系统命令需要的错误const

char而不是字符串。任何人都可以帮助我获得这个系统

命令工作。


我曾经想过要研究一下指向
的指针
字符串并将其放入命令中,这是否在右边

轨道?


您可以给予的任何帮助将不胜感激。


谢谢和问候


John

Hello,

I am writing a script which is using the system() command. In the
script the system command invokes an encryption with system("gpg -f
file.txt").

The problem comes when the script is required to perform this
operation on different files which the user selects via another
function. The name of the filename is stored in a variable as a
string and I can''t get the variable substituted into the ststem
command. I have tried to concatenate the whole command inside the
quotes into one string variable and enter it into the system command
but this brings up the error that the system command requires a const
char and not strings. Can anyone help me out with getting this system
command to work.

I have had the thought of looking into tcreating a pointer to the
string and putting this in the command, would this be on the right
track?

any help you can give would be greatly appreciated.

Thanks and regards

John

推荐答案

Sokar写道:
我正在编写一个使用system()命令的脚本。在
脚本中,system命令用系统调用加密(gpg -f
file.txt)。

当脚本需要执行此操作时出现问题
对用户通过另一个
功能选择的不同文件进行操作。文件名的名称作为
字符串存储在变量中,我无法将变量替换为ststem
命令。我试图将
引号中的整个命令连接成一个字符串变量并将其输入系统命令
但是这会出现系统命令需要const
char的错误而不是字符串。任何人都可以帮助我使用这个系统
命令工作。

我曾经想过要查找指向
字符串并将其放入命令的指针,这会是正确的吗?
I am writing a script which is using the system() command. In the
script the system command invokes an encryption with system("gpg -f
file.txt").

The problem comes when the script is required to perform this
operation on different files which the user selects via another
function. The name of the filename is stored in a variable as a
string and I can''t get the variable substituted into the ststem
command. I have tried to concatenate the whole command inside the
quotes into one string variable and enter it into the system command
but this brings up the error that the system command requires a const
char and not strings. Can anyone help me out with getting this system
command to work.

I have had the thought of looking into tcreating a pointer to the
string and putting this in the command, would this be on the right
track?




...

std :: string onepart(" gpg -f ");

std :: string twopart;

//以某种方式获得twopart ...

twopart =" file.txt" ;

系统((onepart + twopart).c_str());


V



...
std::string onepart("gpg -f ");
std::string twopart;
// obtain twopart somehow ...
twopart = "file.txt";
system((onepart+twopart).c_str());

V


>>这会出现系统命令需要const
>>this brings up the error that the system command requires a const
char而不是字符串的错误。


使用std :: string :: c_str();

我曾考虑过创建一个指向
字符串的指针把它放在命令中,这会是正确的吗?
char and not strings.
Use std::string::c_str();
I have had the thought of looking into tcreating a pointer to the
string and putting this in the command, would this be on the right
track?




不,因为那样你就会把它传递给std: :string *而不是

const char *



No, because then you would be passing it a std::string * and not a
const char *


STL''string''类有一个''。c_str ()''方法返回一个指针

到一个以空字符结尾的字符数组,表示字符串''

的内容。

#include< string>

#include< cstdlib>

int

main()

{

std :: string quine(" perl -e''while(<>){print;}''quine.cpp");

||||

++++ --- +++

//不是真正的quine,|||

//因为它没有打印所有

//它的源代码。


std :: system( quine.c_str());

}

The STL ''string'' class has a ''.c_str()'' method that "Returns a pointer
to a null-terminated array of characters representing the string''s
contents."
#include <string>
#include <cstdlib>
int
main()
{
std::string quine("perl -e ''while(<>) { print; }'' quine.cpp");
||||
++++---+++
// not a real quine, |||
// since it doesn''t print all
// of it''s source code.

std::system( quine.c_str() );
}


这篇关于系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆