->>>关于命令行字符串的分离及重定向,该如何处理

-------------->>>关于命令行字符串的分离及重定向

问1:
从键盘输入一字串string,   写一函数getstr()分离出命令和路径

具体说:去除开头空格.中间空格保留一个,并以中间这个空格(也可以是 '> ')为分界,空格前的放入str1,空格后的放入出str2.末尾的不管它.返回str1,str2

如 "                         cd         c:\       "处理成 "cd   c:\ "   并将 "cd "放入字串str1, "c:\ "放入str2;分离后的串作为形参返到主函数中.
又如:
"dir> c:\xx.txt "       str1为 "dir "     str2为 "c:\xx.txt "           *****
"notepad.exe     "                   str1为空str2为 "notepad.exe "
"c:\123.exe "                   str1为空,str2为 "c:\123.exe "

我写的程序出现的问题是:对str2处理不尽人意,返回主函数中是要么没有,要么乱码.


问2:   写输出内容重定向的功能的思路?

------解决方案--------------------
有现成函数getopt()
------解决方案--------------------
The C++ Standard Library

13.10.3 Redirecting Standard Streams
In the old implementation of the IOStream library, the global streams cin, cout, cerr, and
clog were objects of the classes istream_withassign and ostream_withassign. It was
therefore possible to redirect the streams by assigning streams to other streams. This possibility
was removed from the C++ standard library. However, the possibility to redirect streams was
retained and extended to apply to all streams. A stream can be redirected by setting a stream
buffer.
The setting of stream buffers means the redirection of I/O streams controlled by the program
without help from the operating system. For example, the following statements set things up such
that output written to cout is not sent to the standard output channel but rather to the file
cout.txt:
std::ofstream file ( "cout.txt ");
std::cout.rdbuf (file.rdbuf());
The function copyfmt() can be used to assign all format information of a given stream to
another stream object:
std::ofstream file ( "cout.txt ");
file.copyfmt (std::cout);
std::cout.rdbuf (file.rdbuf());
Caution! The object file is local and is destroyed at the end of the block. This also destroys the
corresponding stream buffer. This differs from the "normal " streams because file streams allocate
their stream buffer objects at construction time and destroy them on destruction. Thus, in this
example, cout can no longer be used for writing. Actually, it cannot even be destroyed safely at
program termination. Thus, the old buffer should always be saved and restored later! The
following example does this in the function redirect():
// io/redirect.cpp
#include <iostream>
#include <fstream>
using namespace std;
void redirect(ostream&);
int main()
{
cout < < "the first row " < < endl;
redirect (cout);
cout < < "the last row " < < endl;