I'm executing my program with the following command:
./myProgram -i test.in -o test.out
Both files are legal and exist.
// 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);
}
I wrote this block in main
in order to redirect cin
and cout
according to char **argv
.
cin
works just fine but cout
does not.
When I take it like that it works:
// 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());
What is causing the problem?