我有2个项目,都在同一解决方案上。
项目1:根据调试输入执行单个操作。 为了简单起见,假设主程序输出调试输入。
Project#2:我想使用通过不同调试运行的for循环来运行Project#1 输入。
如何正确,有效地做到这一点? 据我了解,不建议从Project#2调用Project#1 exe文件。其他任何方式来运行Project#1 :: main,而无需更改Project#1?仅在Project#2中有更改。.
谢谢
高级c ++的新手。
答案 0 :(得分:0)
您不必在单独的项目中执行此操作,您可以使用不同的命令行选项从一个项目中执行所有操作。
对于选项一,您可以通过以下方式运行命令行:pro.exe debug-print1
该项目只是打印参数并退出。
对于第二个选项,您可以创建一个文件,
您可以将所有调试打印文件放入文件中,并在文件的每一行上进行迭代,您只需要将其标记为文件即可,例如使用-f filename
。
下一步是在同一运行中处理多个文件或调试打印,或文件和打印的组合。
因此请考虑以下代码:
#include <string>
#include <iostream>
#include <fstream>
//process a line function:
void proccess_debug_line(const std::string& debug_line)
{
std::cout<<debug_line<<std::endl;
}
//process file by processing each line:
void process_debug_file(const std::string& debug_file_name)
{
std::string debug_line;
std::ifstream inputfile(debug_file_name);
if(!inputfile)
{
std::cerr<<"error openning file: "<< debug_file_name <<std::endl;
}
while(std::getline(inputfile, debug_line))
{
//use the process line
proccess_debug_line(debug_line);
}
}
//argument can be a line, or a file if there is -f before it.
int main(int argc, char **argv)
{
for(int i=1; i<argc; i++)
{
std::string param = argv[i];
if(param[0] == '-')
{
if(param == "-f") // arguments in form -f filename
{
if(i == argc-1 ) // -f is last arg
{
std::cerr<<"Missing argument for " << param << " option."<<std::endl;
}
else //-f filename
{
std::string filename = argv[++i];
process_debug_file(filename);
}
}
else if(param.substr(0,2)== "-f")// argument in form -ffilename can be legal too
{
std::string filename = &(argv[i][2]);
process_debug_file(filename);
}
else
{
std::cerr<<"Unknown option '" << param << "'"<<std::endl;
++i;
}
}
else //a 'regular' parameter (without -f before it) is a debug print
{
proccess_debug_line(param);
}
}
}