다음 명령을 사용하여 프로그램을 실행하고 있습니다. ./myProgram -i test.in -o test.out
Cout과 Cin을 리디렉션하는 방법은 무엇입니까?
두 파일 모두 합법적이며 존재합니다.
// 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);
}
나는
char **argv
에 따라
cin
및
cout
를 리디렉션하기 위해
main
이 블록을 썼다.
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());
가 어떤 문제를 일으키는 : 작동 그렇게 은 내가 촬영하면?
나는 정적 !!!!!!!!!을 잊어 버렸다. 미안하지만 왜 우리가 cin과 main을 다른 기능으로 설정해야하는지 왜 말할 수 있습니까? –