c ++ linux系统命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6506898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ linux system command
提问by Tebe
I have the following problem:
我有以下问题:
I use in my program this function:
我在我的程序中使用这个函数:
system("echo -n 60 > /file.txt");
it works fine.
它工作正常。
But I don't want to have constant value. I do so:
但我不想拥有恒定的价值。我这样做:
curr_val=60;
char curr_val_str[4];
sprintf(curr_val_str,"%d",curr_val);
system("echo -n curr_val_str > /file.txt");
I check my string:
我检查我的字符串:
printf("\n%s\n",curr_val_str);
Yes,it is right.
but system
in this case doesn't work and doesn't return -1. I just print string!
是的,没错。但system
在这种情况下不起作用并且不返回-1。我只是打印字符串!
How can I transfer variable like integer that will be printed in file like integer, but don't string?
如何传输像整数这样的变量,这些变量将像整数一样打印在文件中,但不要字符串?
So I want to have variable int a and I want to print value of a with system function in file. A real path to my file.txt is /proc/acpi/video/NVID/LCD/brightness. I can't write with fprintf. I don't know why.
所以我想要变量 int a 并且我想在文件中使用系统函数打印 a 的值。我的 file.txt 的真实路径是 /proc/acpi/video/NVID/LCD/brightness。我不能用 fprintf 写。我不知道为什么。
采纳答案by Constantinius
you cannot concatenate strings like you are trying to do. Try this:
您不能像尝试那样连接字符串。尝试这个:
curr_val=60;
char command[256];
snprintf(command, 256, "echo -n %d > /file.txt", curr_val);
system(command);
回答by e.dan
#define MAX_CALL_SIZE 256
char system_call[MAX_CALL_SIZE];
snprintf( system_call, MAX_CALL_SIZE, "echo -n %d > /file.txt", curr_val );
system( system_call );
回答by borrible
The system
function takes a string. In your case it's using the text *curr_val_str* rather than the contents of that variable. Rather than using sprintf
to just generate the number, use it to generate the entire system command that you require, i.e.
该system
函数接受一个字符串。在您的情况下,它使用文本 *curr_val_str* 而不是该变量的内容。而不是sprintf
仅用于生成数字,而是使用它来生成您需要的整个系统命令,即
sprintf(command, "echo -n %d > /file.txt", curr_val);
first ensuring that command is large enough.
首先确保命令足够大。
回答by Igor Oks
The command that is actually (erroneously) executed in your case is:
在您的情况下实际(错误地)执行的命令是:
"echo -n curr_val_str > /file.txt"
Instead, you should do:
相反,你应该这样做:
char full_command[256];
sprintf(full_command,"echo -n %d > /file.txt",curr_val);
system(full_command);
回答by gulyan
The correct way would be similar to this:
正确的方法类似于:
curr_val=60;
char curr_val_str[256];
sprintf(curr_val_str,"echo -n %d> /file.txt",curr_val);
system(curr_val_str);
回答by martsbradley
Use snprintf
to avoid security issues.
使用snprintf
以避免安全问题。
回答by Vinicius Kamakura
Just DON'T. :)
不要。:)
Why resort to system()
for such a simple operation?
为什么要system()
进行这么简单的操作?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int write_n(int n, char * fname) {
char n_str[16];
sprintf(n_str, "%d", n);
int fd;
fd = open(fname, O_RDWR | O_CREAT);
if (-1 == fd)
return -1; //perror(), etc etc
write(fd, n_str, strlen(n_str)); // pls check return value and do err checking
close(fd);
}
回答by Mark B
Have you considered using C++'s iostreams facility instead of shelling out to echo
? For example (not compiled):
您是否考虑过使用 C++ 的 iostreams 工具而不是炮轰echo
?例如(未编译):
std::ostream str("/file.txt");
str << curr_val << std::flush;
Alternately, the command you pass to system
must be fully formatted. Something like this:
或者,您传递给的命令system
必须完全格式化。像这样的东西:
curr_val=60;
std::ostringstream curr_val_str;
curr_val_str << "echo -n " << curr_val << " /file.txt";
system(curr_val_str.str().c_str());
回答by marc
What about using std::string
& std::to_string
...
使用std::string
& std::to_string
...
std::string cmd("echo -n " + std::to_string(curr_val) + " > /file.txt");
std::system(cmd.data());