C++ g+中的system()函数+;
这里是我从日志中删除cpp的源代码C++ g+中的system()函数+;,c++,C++,这里是我从日志中删除cpp的源代码 #include <iostream> #include <stdlib.h> // system #include "header.h" #include "ConfigFile.h" using namespace std; int glogs(int answer) { ConfigFile cf("config.txt"); string lang; double dChannel, dCore;
#include <iostream>
#include <stdlib.h> // system
#include "header.h"
#include "ConfigFile.h"
using namespace std;
int glogs(int answer)
{
ConfigFile cf("config.txt");
string lang;
double dChannel, dCore;
lang = (const string)cf.Value("Main_Setting", "LANG");
dChannel = cf.Value("Main_Setting", "CHANNEL");
dCore = cf.Value("Main_Setting", "CORES");
system("clear");
if(lang == "ENG") {
cout << "Remove the all Server logs...\n";
for(int i=1; i < dChannel+1; i++){
for (int c=1; c < dCore+1; c++){
system("cd /usr/home/game2/channel" + i + atoi("/core") + c + atoi("/ && rm -R syslog && rm -R syserr"));
}
}
cout << "delete logs done return to main menu...\n";
system("sleep 10");
system("./start");
return answer;
}
if(lang == "DE"){
cout << "Loesche alle Server logs...\n";
for(int i=1; i < dChannel+1; i++){
for (int c=1; c < dCore+1; c++){
system("cd /usr/home/game2/channel" + i + atoi("/core") + c + atoi("/ && rm -R syslog && rm -R syserr"));
}
}
cout << "loeschen erfolgreich, rurueck zum Hauptmenue...\n";
system("sleep 10");
system("./start");
return answer;
}
}
我已经在堆栈上搜索了几个小时,但我没有找到解决问题的方法。你的线路
system("cd /usr/home/game2/channel" + i + atoi("/core") + c + atoi("/ && rm -R syslog && rm -R syserr"));
应该是这样的
const auto path = "/usr/home/game2/channel" + std::to_string(i) + "/core" + std::to_string(c);
const auto cmd1 = "rm -R " + path + "/syslog";
const auto cmd2 = "rm -R " + path + "/syserr";
system(cmd1.c_str());
system(cmd2.c_str());
编译这段代码需要c++11,我没有测试代码。建议不要使用<代码>系统>代码>,但<代码>未链接< /代码>和<代码> RDIIR < /代码>或提供递归删除文件夹的类和函数的C++库。您希望用<代码> ATOI(“/Cype”)< /COD>实现什么?您不能使用<代码> +<代码>连接<代码> char const */COD>。“cd/usr/home/game2/channel“+i,其中
i
是int
,并没有做您似乎认为的事情。在C语言中,这是指针算法。我不知道您来自哪种语言,也不知道这是从哪种语言移植的,但这不是C中字符串构建的工作方式。这需要更多的时间,而且在Google Fu中值得花时间。@KoriNatur使用std::ostringstream
来构建命令以传递到system()
函数中。整个system()
调用本身就是毫无意义的。如果首先尝试将绝对不是数字的字符串转换为整数,您打算实现什么?
const auto path = "/usr/home/game2/channel" + std::to_string(i) + "/core" + std::to_string(c);
const auto cmd1 = "rm -R " + path + "/syslog";
const auto cmd2 = "rm -R " + path + "/syserr";
system(cmd1.c_str());
system(cmd2.c_str());