我正在使用以下命令执行我的程序:
./myProgram -i test.in -o test.out
这两个文件都是合法的并且存在。
// run all over the arguments and set the cin and cout if needed
for (int i = 1; i < argc; i= i+2)
{
int j = i+1;
// loop over each pairs of arguments
do
{
// set cin
if(argv[i] == "-i")
{
static std :: ifstream s_inF(argv[j]);
std :: cin.rdbuf(s_inF.rdbuf());
break;
}
//set cout
if(argv[i] == "-o")
{
std::ofstream out(argv[j]);
std::cout.rdbuf(out.rdbuf());
break;
}
// in order to search for the other case
// (example:X.out -i)
int temp = i;
i = j;
j = temp;
}while(i>j);
}
我写这个块是main
为了重定向cin
并cout
根据char **argv
.
cin
工作得很好,但cout
没有。当我这样认为它有效时:
// run all over the arguments and set the cin and cout if needed
for (int i = 1; i < argc; i= i+2)
{
int j = i+1;
// loop over each pairs of arguments
do
{
// set cin
if(argv[i] == "-i")
{
static std :: ifstream s_inF(argv[j]);
std :: cin.rdbuf(s_inF.rdbuf());
break;
}
//set cout
if(argv[i] == "-o")
break;
// in order to search for the other case
// (example:X.out -i)
int temp = i;
i = j;
j = temp;
}while(i>j);
}
std::ofstream out(argv[4]);
std::cout.rdbuf(out.rdbuf());
是什么导致了问题?